Skip to main content
GameDev.net gamedev.net
🔒 Locked

const-correctness

Started by altra4u Oct 18, 2008 at 5:47 PM 7 replies 1.6k views
Original Post
altra4u
altra4u
Hi, I know the benefits of having const-correct classes and functions but what would be its overhead or drawback? Also I find that something like const class test { int a; }; is allowed.What would be the meaning of this class?You can even create an object of class test and assign its data member a some value. Thx
dgrat
dgrat
First, why would you ever want to have the ability to change data in a class that's declared const? This gets at the heart of what constness means, and there are two ways of thinking about it. One idea is that of "bitwise constness", which basically means that a const class should have exactly the same representation in memory at all times. Unfortunately (or fortunately), this is not the paradigm used by the C++ standard; instead, C++ uses "conceptual constness". Conceptual constness refers to the idea that the output of the const class should always be the same. This means that the underlying data might change as long as the fundamental behavior remains the same. (In essence, the "concept" is constant, but the representation may vary.)

http://www.cprogramming.com/tutorial/const_correctness.html
SiCrane
SiCrane
Whenever you see
foo class Something {} bar;

It's the same as
class Something {};foo Something bar;

Or in the case of
const class test{int a;};

The same as
class test {  int a;};const test;

const test; by itself is basically a pointless, but legal C++ statement, in the same way const int; by itself is a pointless but legal C++ statement.
scjohnno
scjohnno
Const-correctness has no runtime overhead as all checking is done at compile-time.
altra4u
altra4u
Quote:
Original post by scjohnno
Const-correctness has no runtime overhead as all checking is done at compile-time.


Yeah.Thats what I could think of.That in the worst situation it will have compile-time overhead as it will try to ensure that const-correctness has been maintained.Thnks all you guys
cignox1
cignox1
const can also enhance performance, allowing the compiler to avoid the creation of temporary variables that (if const'ed) would never vary. Ie., As far as I know:

vector3d v = negate(v);

where

const vector3d negate(const vector3d& par)
{
return vector3d(-par.x, -par.y, -par.z);
}

in this case no temporary variable will be created when the function returns.
This is what they told me, so I hope that it is correct :-)
RDragon1
RDragon1
Quote:
Original post by cignox1
const can also enhance performance, allowing the compiler to avoid the creation of temporary variables that (if const'ed) would never vary. Ie., As far as I know:

vector3d v = negate(v);

where

const vector3d negate(const vector3d& par)
{
return vector3d(-par.x, -par.y, -par.z);
}

in this case no temporary variable will be created when the function returns.
This is what they told me, so I hope that it is correct :-)


That optimization can be applied even if the return type is non-const.

See: http://www.gotw.ca/gotw/081.htm
cignox1
cignox1
Quote:
Original post by RDragon1
Quote:
Original post by cignox1
const can also enhance performance, allowing the compiler to avoid the creation of temporary variables that (if const'ed) would never vary. Ie., As far as I know:

vector3d v = negate(v);

where

const vector3d negate(const vector3d& par)
{
return vector3d(-par.x, -par.y, -par.z);
}

in this case no temporary variable will be created when the function returns.
This is what they told me, so I hope that it is correct :-)


That optimization can be applied even if the return type is non-const.

See: http://www.gotw.ca/gotw/081.htm


Oh, ok, thank you for this precisation :-)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.