Question about constant member functions

Discussion in 'C++' started by Sunny, Mar 7, 2007.

  1. Sunny

    Sunny Guest

    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
    Sunny, Mar 7, 2007
    #1
    1. Advertising

  2. On 7 Mar, 12:32, "Sunny" <> 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #2
    1. Advertising

  3. Sunny

    Gavin Deane Guest

    On 7 Mar, 12:57, "Erik Wikström" <> 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
    Gavin Deane, Mar 7, 2007
    #3
  4. On 7 Mar, 15:05, "Gavin Deane" <> wrote:
    > On 7 Mar, 12:57, "Erik Wikström" <> 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #4
  5. Sunny

    Gavin Deane Guest

    On 7 Mar, 14:54, "Erik Wikström" <> wrote:
    > On 7 Mar, 15:05, "GavinDeane" <> wrote:
    >
    > > On 7 Mar, 12:57, "Erik Wikström" <> 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
    Gavin Deane, Mar 8, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. srinivas reddy

    constant member functions

    srinivas reddy, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    400
    Andrey Tarasevich
    Jul 14, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,027
    Fraser Ross
    Aug 14, 2004
  3. blueblueblue2005

    constant member functions of a class

    blueblueblue2005, Jun 30, 2005, in forum: C++
    Replies:
    3
    Views:
    380
    blueblueblue2005
    Jun 30, 2005
  4. Hicham Mouline
    Replies:
    0
    Views:
    424
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    406
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page