confusing statement in Deitel book

Discussion in 'C++' started by blueblueblue2005, Jun 28, 2005.

  1. Hi, I am reading the const function definition part in Deitel book, it
    says :

    Defining as const a member function that calls a non-const member
    function of the class on the same instance of the class is a syntax
    error.

    what does "on the same instance of the class" mean? if I have a class
    like the following:

    class T{
    private:
    int x;
    public:
    int getX() const { return x; }
    };

    here, getX is defined as const, but it access the non-const member x,
    and it is legal. so what Deitel mean by saying above statement?
     
    blueblueblue2005, Jun 28, 2005
    #1
    1. Advertising

  2. blueblueblue2005

    John Carson Guest

    "blueblueblue2005" <> wrote in message
    news:
    > Hi, I am reading the const function definition part in Deitel book, it
    > says :
    >
    > Defining as const a member function that calls a non-const member
    > function of the class on the same instance of the class is a syntax
    > error.
    >
    > what does "on the same instance of the class" mean? if I have a class
    > like the following:
    >
    > class T{
    > private:
    > int x;
    > public:
    > int getX() const { return x; }
    > };
    >
    > here, getX is defined as const, but it access the non-const member x,
    > and it is legal. so what Deitel mean by saying above statement?


    The quote you gave refers to calling a non-const member *function*. x is not
    a function. Suppose you had:


    class T{
    private:
    int x;
    public:
    void DoSomething() // non-const member function
    {}
    int getX() const
    {
    DoSomething();
    return x;
    }
    };

    Now you have an error.

    --
    John Carson
     
    John Carson, Jun 28, 2005
    #2
    1. Advertising

  3. Oh, yeah, you are right, I just found when I read that statement again.
    Thanks a lot, John!

    Holly
     
    blueblueblue2005, Jun 28, 2005
    #3
  4. On 2005-06-28, blueblueblue2005 <> wrote:
    > Hi, I am reading the const function definition part in Deitel book, it
    > says :
    >
    > Defining as const a member function that calls a non-const member
    > function of the class on the same instance of the class is a syntax
    > error.
    >
    > what does "on the same instance of the class" mean? if I have a class
    > like the following:
    >
    > class T{
    > private:
    > int x;
    > public:
    > int getX() const { return x; }
    > };
    >
    > here, getX is defined as const, but it access the non-const member x,
    > and it is legal. so what Deitel mean by saying above statement?


    They mean this:

    >

    class T{
    private:
    int x;
    public:
    int getX() const {
    foo(); // illegal, because this is invoked on the same instance
    T a;
    a.foo(); // this is OK -- a is a different (and non-const) instance
    return x;
    }
    void foo() { } // non-const
    };

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Jun 28, 2005
    #4
  5. "blueblueblue2005" <> wrote in message
    news:...

    > Hi, I am reading the const function definition part in Deitel book, it
    > says :


    > Defining as const a member function that calls a non-const member
    > function of the class on the same instance of the class is a syntax
    > error.


    Here's an example of what I think it means:

    class Foo {
    public:
    void f(); // a non-const member function
    void g() const { f(); }
    };

    This code should indeed fail to compile, but syntax has nothing to do with
    it. Rather, the definition of g is equivalent to

    void g() const { this->f(); }

    Within the body of a const member function, "this" is a pointer to const.
    Therefore, trying to evaluate this->f() violates the semantic constraint
    that says that you cannot use a pointer to const to call a const member
    function.

    If a book says that violating such a constraint is a syntax error, I wonder
    what other misconceptions it shows.
     
    Andrew Koenig, Jun 28, 2005
    #5
  6. "Andrew Koenig" <> wrote in message
    news:rKcwe.1042272$...
    > "blueblueblue2005" <> wrote in message
    > news:...

    <<snip>>
    > This code should indeed fail to compile, but syntax has nothing to do with
    > it. Rather, the definition of g is equivalent to
    >
    > void g() const { this->f(); }
    >
    > Within the body of a const member function, "this" is a pointer to const.
    > Therefore, trying to evaluate this->f() violates the semantic constraint
    > that says that you cannot use a pointer to const to call a const member
    > function.


    Did you mean here "cannot use a pointer to const to call a non-const
    member"?
    I would have thought a const member would be okay.
    --
    Gary
     
    Gary Labowitz, Jun 28, 2005
    #6
  7. "Gary Labowitz" <> wrote in message
    news:...

    >> Within the body of a const member function, "this" is a pointer to const.
    >> Therefore, trying to evaluate this->f() violates the semantic constraint
    >> that says that you cannot use a pointer to const to call a const member
    >> function.


    > Did you mean here "cannot use a pointer to const to call a non-const
    > member"?


    Yes I did; thanks for the correction.

    The main point remains: Type errors are not generally considered syntax
    errors.
     
    Andrew Koenig, Jun 29, 2005
    #7
    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. Xandro
    Replies:
    1
    Views:
    1,264
    Michael Scovetta
    Feb 23, 2004
  2. George
    Replies:
    7
    Views:
    505
    Roger Leigh
    Apr 13, 2004
  3. __frank__

    Book advice:Deitel & Deitel

    __frank__, Oct 15, 2005, in forum: C Programming
    Replies:
    14
    Views:
    685
    Randy Howard
    Oct 17, 2005
  4. Pearson & prentice hall solutions

    Deitel &Deitel How to Program C++ 6th E Code solutions

    Pearson & prentice hall solutions, Apr 27, 2009, in forum: C++
    Replies:
    0
    Views:
    369
    Pearson & prentice hall solutions
    Apr 27, 2009
  5. Pearson & prentice hall solutions
    Replies:
    2
    Views:
    906
    Ian Collins
    May 2, 2009
Loading...

Share This Page