-
May 4th, 2010, 11:06 AM
#1
General C++ question about const
Hi,
I am not a C++ programmer but I am currently attempting to port some C++ code and have hit the following line:
virtual int GetGlobalStateType () const = 0;
I was wondering what the overall construct of the line implied. I THINK that
virtual - means that it can essentially be overriden?
int - the type returned by this method? (god I hope this is right)
GetGlobalStateType () - the method name? (god I hope this is right too lol)
cont - OK, this is the bit that I don't know....
It is that it is simply assigning the default value of the return to 0 and because that default never changes that it is infact a constant value??
Any clarification would be cool, and I probably have more on the way, this code is all smart pointers and cool crazy gubbins that don't exist in my usual language
Regards
RipX
-
May 4th, 2010, 11:13 AM
#2
Re: General C++ question about const
It means that the method (or more specifically, the methods overriding it, since it's pure virtual) is not allowed to modify the object it is operating on. For the duration of the method call, all members of the object are treated as if they're const members (unless they're declared mutable).
This must be done to allow a method to be called on a const object, or via a const reference to an object.
-
May 4th, 2010, 11:13 AM
#3
Re: General C++ question about const
const after a member function is a contract that that method will not effect any changes on the non-mutable parts of an object.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 4th, 2010, 11:17 AM
#4
Re: General C++ question about const
Originally Posted by RipX
virtual - means that it can essentially be overriden?
int - the type returned by this method? (god I hope this is right)
GetGlobalStateType () - the method name? (god I hope this is right too lol)
Yes.
I didn't really understand you explanation about what you thought const did, so here's my recap.
What you have is a method, which means it is called on objects:
Code:
myObject.GetGlobalStateType ();
or
myObjectPointer->GetGlobalStateType();
As such, the current object is implicitly a parameter to your method. Adding const after a method name is actually a qualifier to the "this" argument. It is not the method that is constant, the object on which the method is called.
Doing this implies:
- You garantee to your users you will not be changing *this (aka myObject).
- You will not be able to modify *this in your method.
- You will be allowed to call GetGlobalStateType on a const object.
Code:
class myClass
{
void modify();
void notModify() const;
}
myClass a;
const myClass b;
a.modify(); //Fine,
a.notModify(); //Fine
b.modify(); //<-- Error. modify can change b, yet b is const.
b.notModify(); //Fine, b is const, but notModify promises not to change it.
There. As a general rule, that const keywords follows the exact same rules as the one inside the parameter list, but applied to the this "parameter".
The "= 0" afterwards means the method is a pure virtual, and has nothing to do with constness.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 4th, 2010, 11:24 AM
#5
Re: General C++ question about const
Wow...three very fast responces, thank you so much. I understand that now.
RipX
-
May 5th, 2010, 10:49 AM
#6
Re: General C++ question about const
It should also be noted that you can overload a method by its constness and non-constness!
Code:
class Test
{
public:
void get();
void get() const;
If get method is called on const-object, it would call const-version, otherwise normal version.
Why do you need to do that, you might ask! Well, for example you are writing a String class, having operator [] overloaded. Do you want this to be possible:
Code:
String str;
...
str[0] = 'X';
If so, you need to return reference of respective character, which CAN be modified. If the same operator is called from const object, you just return character:
Code:
class String
{
public:
char& operator[](int nIndex);
char operator[](int nIndex) const;
};
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|