Const memebr function behaviour.

Discussion in 'C++' started by Rusty, May 18, 2007.

  1. Rusty

    Rusty Guest

    Can somebody can explain the following behaviour. I have a small code
    as below:

    class B
    {
    public:
    int b;
    B () { b = 0; };
    void foo2 (int h) { b = h; } // non-const function
    };

    class A
    {
    public:
    int a;
    A () { cout << "Inside default constructor" << endl; b = new B
    ();}

    B* b;

    void bar () const { b->foo2 (3); } // problem. how can this
    compile ?
    };


    void foo (const A* a)
    {
    a->bar ();
    }

    int
    main (int argc, char** argv)
    {
    A a1;
    foo (&a1);
    }

    My concern is how could this code compile when in bar () function of
    class A, I am calling non const function on pointer b ?

    If instead of pointer, b is an object of type B, I get correct error
    saying that inside const member function of class A, I cannot call non
    const member function of B.

    Thanks in advance,

    -Aseem.
     
    Rusty, May 18, 2007
    #1
    1. Advertising

  2. Rusty

    Rolf Magnus Guest

    Rusty wrote:

    > Can somebody can explain the following behaviour. I have a small code
    > as below:
    >
    > class B
    > {
    > public:
    > int b;
    > B () { b = 0; };
    > void foo2 (int h) { b = h; } // non-const function
    > };
    >
    > class A
    > {
    > public:
    > int a;
    > A () { cout << "Inside default constructor" << endl; b = new B
    > ();}
    >
    > B* b;
    >
    > void bar () const { b->foo2 (3); } // problem. how can this
    > compile ?
    > };
    >
    >
    > void foo (const A* a)
    > {
    > a->bar ();
    > }
    >
    > int
    > main (int argc, char** argv)
    > {
    > A a1;
    > foo (&a1);
    > }
    >
    > My concern is how could this code compile when in bar () function of
    > class A, I am calling non const function on pointer b ?


    Why would that be a problem?

    > If instead of pointer, b is an object of type B, I get correct error
    > saying that inside const member function of class A, I cannot call non
    > const member function of B.


    But you have a pointer. The constness of a pointer isn't related to the
    constness of the object it points to.
     
    Rolf Magnus, May 18, 2007
    #2
    1. Advertising

  3. On 18 Maj, 16:15, Rusty <> wrote:
    > Can somebody can explain the following behaviour. I have a small code
    > as below:
    >
    > class B
    > {
    > public:
    > int b;
    > B () { b = 0; };
    > void foo2 (int h) { b = h; } // non-const function
    >
    > };
    >
    > class A
    > {
    > public:
    > int a;
    > A () { cout << "Inside default constructor" << endl; b = new B
    > ();}
    >
    > B* b;
    >
    > void bar () const { b->foo2 (3); } // problem. how can this
    > compile ?
    >
    > };
    >
    > void foo (const A* a)
    > {
    > a->bar ();
    >
    > }
    >
    > int
    > main (int argc, char** argv)
    > {
    > A a1;
    > foo (&a1);
    >
    > }
    >
    > My concern is how could this code compile when in bar () function of
    > class A, I am calling non const function on pointer b ?
    >
    > If instead of pointer, b is an object of type B, I get correct error
    > saying that inside const member function of class A, I cannot call non
    > const member function of B.


    If you have a B member and calls on the function that instance will be
    const (since the function is const). However when you have a pointer
    and call the function the *pointer* becomes const and not the object
    it points to.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 18, 2007
    #3
  4. Rusty

    *PaN!* Guest

    "Rusty" <> wrote in message
    news:...

    > B* b;
    > void bar () const { b->foo2 (3); } // problem. how can thiscompile ?


    'b = 0;' would be illegal there.
    Because you are not changing the pointer, but the pointed object, which is
    not part of your object (the pointer is).

    --*PaN!*
     
    *PaN!*, May 18, 2007
    #4
  5. Rusty

    Salt_Peter Guest

    On May 18, 10:15 am, Rusty <> wrote:
    > Can somebody can explain the following behaviour. I have a small code
    > as below:
    >
    > class B
    > {
    > public:
    > int b;
    > B () { b = 0; };
    > void foo2 (int h) { b = h; } // non-const function
    >
    > };
    >
    > class A
    > {
    > public:
    > int a;
    > A () { cout << "Inside default constructor" << endl; b = new B
    > ();}
    >
    > B* b;
    >
    > void bar () const { b->foo2 (3); } // problem. how can this
    > compile ?
    >
    > };
    >
    > void foo (const A* a)
    > {
    > a->bar ();
    >
    > }
    >
    > int
    > main (int argc, char** argv)
    > {
    > A a1;
    > foo (&a1);
    >
    > }
    >
    > My concern is how could this code compile when in bar () function of
    > class A, I am calling non const function on pointer b ?
    >
    > If instead of pointer, b is an object of type B, I get correct error
    > saying that inside const member function of class A, I cannot call non
    > const member function of B.
    >
    > Thanks in advance,
    >
    > -Aseem.


    The member function bar () const is not modifying the instance of
    class A in any way.
    I see a new B in A's ctor but no corresponding deallocation.
     
    Salt_Peter, May 18, 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. Replies:
    11
    Views:
    1,108
  2. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,122
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    542
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page