Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about constant member functions

Reply
Thread Tools

Question about constant member functions

 
 
Sunny
Guest
Posts: n/a
 
      03-07-2007
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 12:32, "Sunny" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 12:57, "Erik Wikström" <(E-Mail Removed)> wrote:
>[*] 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

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-07-2007
On 7 Mar, 15:05, "Gavin Deane" <(E-Mail Removed)> wrote:
> On 7 Mar, 12:57, "Erik Wikström" <(E-Mail Removed)> wrote:
>
> >[*] 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

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      03-08-2007
On 7 Mar, 14:54, "Erik Wikström" <(E-Mail Removed)> wrote:
> On 7 Mar, 15:05, "GavinDeane" <(E-Mail Removed)> wrote:
>
> > On 7 Mar, 12:57, "Erik Wikström" <(E-Mail Removed)> wrote:

>
> > >[*] 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
constant member functions of a class blueblueblue2005 C++ 3 06-30-2005 12:26 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
constant member functions srinivas reddy C++ 2 07-14-2003 10:52 PM



Advertisments