confusing statement in Deitel book

B

blueblueblue2005

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?
 
J

John Carson

blueblueblue2005 said:
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.
 
B

blueblueblue2005

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

Holly
 
D

Donovan Rebbechi

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,
 
A

Andrew Koenig

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.
 
G

Gary Labowitz

Andrew Koenig said:
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.
 
A

Andrew Koenig

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top