473,463 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

confusing statement in Deitel book

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?

Jul 23 '05 #1
6 1558
"blueblueblue2005" <zh******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com
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

Jul 23 '05 #2
Oh, yeah, you are right, I just found when I read that statement again.
Thanks a lot, John!

Holly

Jul 23 '05 #3
On 2005-06-28, blueblueblue2005 <zh******@gmail.com> 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/
Jul 23 '05 #4
"blueblueblue2005" <zh******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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.
Jul 23 '05 #5
"Andrew Koenig" <ar*@acm.org> wrote in message
news:rK**********************@bgtnsc05-news.ops.worldnet.att.net...
"blueblueblue2005" <zh******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com... <<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
Jul 23 '05 #6
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:H8********************@comcast.com...
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.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: George | last post by:
I hate to bug you old timers and pros that use C++. So if you will spare me a small moment then I be out of your hair. I am taking a C++ class and got this CD with the book. You would think...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
14
by: __frank__ | last post by:
What do you think about the following book: "C How to Program", 4th edition, ISBN: 0131426443 I'm a novice. Thanks in advance.
6
by: nembo kid | last post by:
According to accu.org, Deitel's "How to program" is not reccomended. I don't agree. I have the 3rd ed. and I find it ok. What do you think about this book? Thanks in advance.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.