By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Question about constant member functions

P: n/a
Hi,
What are the compiler optimizations that are applicable if a member
function is declared const ? What kind of better code is generated for
aconst function in terms of execution speed.
Thanks

Mar 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 7 Mar, 12:32, "Sunny" <shiladitya.bis...@gmail.comwrote:
Hi,
What are the compiler optimizations that are applicable if a member
function is declared const ? What kind of better code is generated for
aconst function in terms of execution speed.
Non that I'm aware of to my knowledge it's more of a tool for the
developer. When I can a const member of an object there are certain
assumptions I can make, such that the state of the object does not
change[*] which can have some advantages, especially when trying to
verify that a program works correctly. Having said that it might be
possible that there are some optimizations that can be made also.
[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.

--
Erik Wikström

Mar 7 '07 #2

P: n/a
On 7 Mar, 12:57, "Erik Wikström" <eri...@student.chalmers.sewrote:
[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.
Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.

Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.

Gavin Deane

Mar 7 '07 #3

P: n/a
On 7 Mar, 15:05, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 7 Mar, 12:57, "Erik Wikström" <eri...@student.chalmers.sewrote:
[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.

Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.

Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.
I meant something like this:

sruct Foo {
int i;
void bar() const;
}

void Foo::bar() {
Foo* fp = const_cast<Foo*>(this);
fp->i = 3;
}

int main() {
Foo f;
f.i = 3;
f.bar();
}

Now, given that bar is declared const it should normally not be able
to change the state of f, but using const_cast it's possible. I
believe this to be legal, of course, had I declared f const as well
then it would be UB.

--
Erik Wikström

Mar 7 '07 #4

P: n/a
On 7 Mar, 14:54, "Erik Wikström" <eri...@student.chalmers.sewrote:
On 7 Mar, 15:05, "GavinDeane" <deane_ga...@hotmail.comwrote:
On 7 Mar, 12:57, "Erik Wikström" <eri...@student.chalmers.sewrote:
[*] Once can make modifications even in a const member using
const_cast, but it's not a good practice.
Not if the object was declared const one can't. Any attempt to modify
a const object leads to undefined behaviour.
Perhaps that's what you meant, but "not a good practice" didn't seem a
strong enough deterrant to me.

I meant something like this:

sruct Foo {
int i;
void bar() const;

}

void Foo::bar() {
Foo* fp = const_cast<Foo*>(this);
fp->i = 3;

}

int main() {
Foo f;
f.i = 3;
f.bar();

}

Now, given that bar is declared const it should normally not be able
to change the state of f, but using const_cast it's possible. I
believe this to be legal, of course, had I declared f const as well
then it would be UB.
That's my understanding too.

Gavin Deane

Mar 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.