Sign in to follow this  

const keyword after function declaration?

This topic is 4344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It means that no member field of the class is changed by the method getParent(). In other words getParent() is neither a "manipulator" (named so e.g. by Booch) nor has it any side-effect on the object.


EDIT: But see my post below for some details, since the explanation above is only one side.

[Edited by - haegarr on January 26, 2006 3:29:41 AM]

Share this post


Link to post
Share on other sites
It means that when you make an object of the class type that is const, like:

const MyClass myClassObject;

When accessing its members you can only call the methods with the const after the declaration.

In this case the idea of const is to show the user of your class which methods do and don't alter the state of the object.

For example:


class MyClass
{
private:
unsigned int count;

public:
unsigned int GetCount1() const;
unsigned int GetCount2();
};

Here, if you declared a const object of type MyClass, you could call GetCount1() only. However you could call both on a non-const object. Ofcourse, in this case GetCount2() should actually be declared const.

One thing to note is that the const member function should not alter the state of the class.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by haegarr
It means that no memeber field of the class is changed by the method getParent().

Okay, so basically if I tried to edit a member variable in that function I would get a compiler error?

Thanks for the fast reply by the way! :)

edit: also thanks to Dave.

I guess that's pretty nifty but I doubt I will remember to use it much... maybe when I'm more experienced.

Share this post


Link to post
Share on other sites
edited...

Umm, i would suggest getting into the habit of using it now. It isn't really a necessary part of the language to know but i have found that few people on these forums use it properly.

Dave

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Rule of thumb: Always use it when possible. Otherwise you will end up ripping your hair when you declare a const instance of the object, and can't call any methods on it.

Share this post


Link to post
Share on other sites
Okay, lets me dive a little more into depth to show the other side also. Normally the compiler will complain a writing access to any member field (and also the invocation of a non-const routine) from inside a const routine. Be aware that the following may confuse, so you are free to ignore it for now ;)

But in fact, declaring a method const means that the state as seen from the outside of an object of that class does not change if that routine is executed. The state in this sense is the entirety of all accessible values (i.e. normally return values of even such const "accessor" routines).

Well, why have I chosen such an artificial looking term like "state as seen from the outside"? Because there is also an inner state given by the entirety of all member fields. As you may assume, the inner state and the outer state in this sense need not be the same. Situations exist (seldom but nevertheless) where it is necessary (or at least convenient) to change the inner state w/o changing the outer state. In such case you have to alter a member field within a const routine. That could be done this way:

class AClass {

public: // accessors

type accessor() const {
// here _field1 could be altered, but not _field2
}

private: // fields

mutable type _field1;

type _field2;

};


Notice that mutable keyword that virtually removes all constness from routines for that single member field.

You should, of course, use mutable fields if and only if really necessary. But I also recommend you to use const routines where possible, as already suggested by the AP above.

Share this post


Link to post
Share on other sites
Okay, an example: Think of complex numbers. A complex number could be given as a tuple of real/imaginary values, or as a tuple of absolute/angle values. Assume you want not to store both representations, but you want to offer both to clients. So you could do like this (incomplete example):

class Complex {

public: // setters & getters

void setReIm(float re,float im) {
this->val1 = re;
this->val2 = im;
this->isReImNotAbsArg = true;
}

void setAbsArg(float abs,float arg) {
this->val1 = abs;
this->val2 = arg;
this->isReImNotAbsArg = false;
}

void getRe() const {
makeReIm();
return this->val1;
}

void getIm() const {
makeReIm();
return this->val2;
}

void getAbs() const {
makeAbsArg();
return this->val1;
}

void getArg() const {
makeAbsArg();
return this->val2;
}

private:

void makeReIm() const {
if(!this->isReImNotAbsArg) {
float re = this->val1 * cos(this->val2);
float im = this->val1 * sin(this->val2);
this->val1 = re;
this->val2 = im;
this->isReImNotAbsArg = true;
}
}

void makeAbsArg() const {
if(this->isReImNotAbsArg) {
float abs = sqrt(this->val1 * this->val1 + this->val2 * this->val1);
float arg = atan2(this->val2, this->val1);
this->val1 = abs;
this->val2 = arg;
this->isReImNotAbsArg = false;
}
}

mutable float abs;

mutable float arg;

mutable bool isReImNotAbsArg;

};


As you could see, the inner state may change (either the one or else the other representation is stored) while the outer state stay the same (besides any numerical resolution issues).

There are other possible solutions for this particular problem, of course, but this is just an example :)

Share this post


Link to post
Share on other sites

This topic is 4344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this