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
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.
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.
Re: General C++ question about const
Quote:
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.
Re: General C++ question about const
Wow...three very fast responces, thank you so much. I understand that now. :)
RipX
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;
};