constant function

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

constant function

Post by arras »

What does declaring function constant do?

For example:

Code: Select all

const core::aabbox3df& getBoundingBox() const 
(I mean const after function declaration)

Also what benefit does have returning value as const reference instead of copy?

Like

Code: Select all

const int& somefunction()
{
   return some member int;
}
Against

Code: Select all

int somefunction()
{
   return some member int;
}
Is it faster?
kohansey
Posts: 14
Joined: Tue Apr 01, 2008 3:18 am

Post by kohansey »

by declaring a function const, basically means that the function can be called within another const function

By having a function return a const value, it is an "almost" guarantee that the object returned will not get changed outside the class for which it originated. I say almost, because you could always do a const_cast and cast away the const.

Read Effective C++: 50 Specific Ways to Improve Your Programs and Design
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

kohansey wrote:by declaring a function const, basically means that the function can be called within another const function

By having a function return a const value, it is an "almost" guarantee that the object returned will not get changed outside the class for which it originated. I say almost, because you could always do a const_cast and cast away the const.

Read Effective C++: 50 Specific Ways to Improve Your Programs and Design
Well you were right on the second part, but not the first part.

The const keyword after a function means that the if the user were to create an instance of that class, they would still be able to use that method. Basically const means that that method isn't modifying any data in the class. But there is a catch to this! Suppose you want to have a const method that modifies only a specific piece of data. Well then you declare that data mutable, and it will be able to be manipulated by any function whether the class instance is constant or not.

Examples:

Code: Select all

class A
{
   int num;
   mutable int modify_me;

    int getNum() { return num; }
    void setModifyMe(int newInt) const { modify_me = newInt; }
};
This is a big no-no in this case. If getNum() was const, then this code would work perfectly fine.

Code: Select all

const class A;
A.getNum();
This words, because modify_me is mutable.

Code: Select all

const class A;
A.setModifyMe(5);
So basically const makes sure that the data within the class is read-only. (With exceptions explained above.)
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I didn't knew about the mutable thing, cool.

P.S
By making a method const it also means the this pointer is constant in those methods as far as I know.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Returning a const reference is usually done for performance reasons, in order to avoid a copy return. This does not really help for the PODs like int or float, but it's a good idea for structs and objects. However, you cannot use this way if you create a temporary object in your ode and return that by (const) reference, because it already ceased when the method was left. So returning a const& is usually replacing a copy-return, it's not meant to make reference returns safer.
const methods declare the method to leave the internal object state unchanged. This is ensured by the compiler by making the this pointer const as MasterGod said. The good thing with these methods is that they can also be called on const pointers and references. Hence, if your method gets a const A* or const A&, you can still use the const methods on those objects, while the other methods cannot be used (pretty much like Halifax said). That's also why Irrlicht changed its methods - programs which paid some attention to making parameters const where possible couldn't use Irrlicht methods anymore, so they had to make their methods unsafer, or use const casts.
Also, const methods can give compilers the possibility to optimize their usage. This can lead to faster compilation or enhanced optimizations. Those effects aren't too visible for most tools, though. Still, the design is much better if you use const methods where it makes sense. Especially for interface definitions you have to pay much attention to the question if some method should become const. Not every method that leaves the object unaltered should be const (but most probably should), and sometimes you have a const method which still needs to alter some internal state.
That's where mutable attributes can come in. However, if you use the mutable qualifier for exposed variables (doesn't matter if public, or just accessible by getters and setters), you'll lose everything from the constification of the other methods. Instead, you should only make attributes mutable which are only accessed from within the object's methods, i.e. an inner state variable. So mutables aren't bad by default, they can just easily be used in wrong places. But if you choose to make an attribute public it can just as easily ruin your design. It's just no simple task to make a properly design application.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Well, thats an Answer :) ...thanks a lot to all. All is clear now.
Just one small question, I assume inserting objects in to function by reference (constant or not) is probably also faster (no copy) right?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Great answer indeed.
About the ref thing, arras, yes. Passing values by reference means the copy constructor isn't called and the copy overhead does not happen.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: constant function

Post by rogerborg »

arras wrote:What does declaring function constant do?
Generates a compiler error.

Function != method.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Method is Java speak. In C++ they are called member functions and thus are functions too. ;)
Anyway non-member functions can't be declared const of course, as it wouldn't make any sense in this context.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Saturn wrote:Method is Java speak. In C++ they are called member functions and thus are functions too. ;)
Alyson Hannigan is a hot broad, and my wife is a broad, thus my wife is Alyson Hannigan.

Eh, method is everyone but Stroustrup speak. However, I'll take my bitch-slapping like a girlie-man on this occasion, since we are talking specifically about C++ syntax.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Thanks for answers :)

Eckel also doesn't speak about methods. And to me it does seem pretty logical. Method is just member function, there is really no other difference just scope. You have variables and functions. And you have member functions and member variables. As variable build in to class does not become something else so does not function. If I miss something important please correct me.

Bye the way, I do not care what name we use as long as we understand each other :wink: Function is just my personal preference. But I am open to change my mind if there is important and valid argument against it.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Nah, I was just nit picking because these selfish bast^W fine fellows already grabbed all the good point. ;)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply