473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Using "const" : why does compile fail


Could someone please explain to me why the code segment

class FOO {
public:
double *begin();
};

void bar(const FOO &foo) {
foo.begin();
}

gives the compilation errors

foo.cc(7): error: the object has cv-qualifiers that are not compatible
with the member function
object type is: const FOO
foo.begin();
^

with Intel icc 7.1 and

foo.cc: In function `void bar(const FOO&)':
foo.cc:7: error: passing `const FOO' as `this' argument of `double*
FOO::begin()' discards qualifiers

with GNU C++ 3.3.1. I cannot figure out what either means. The errors
go away when I remove the "const" keyword. I've obviously mangled the
advice on page 146 of Stroustroup, but don't understand how.
Jul 19 '05 #1
5 16932
WW
Jim West wrote:
Could someone please explain to me why the code segment

class FOO {
public:
double *begin();
double *begin() const;

You need to tell that the function can be used on const objects.
};

void bar(const FOO &foo) {
foo.begin();
}

gives the compilation errors

foo.cc(7): error: the object has cv-qualifiers that are not compatible
with the member function
object type is: const FOO
foo.begin();
^


--
WW aka Attila
Jul 19 '05 #2
Jim West wrote:
Could someone please explain to me why the code segment

class FOO {
public:
double *begin();
};

void bar(const FOO &foo) {
foo.begin();
}

gives the compilation errors

foo.cc(7): error: the object has cv-qualifiers that are not compatible
with the member function
object type is: const FOO
foo.begin();
^


You are calling a non-const method on a const object.

Jul 19 '05 #3
On 13 Oct 2003 21:32:59 GMT, Jim West <eg***********@yahoo.com> wrote:

Could someone please explain to me why the code segment

class FOO {
public:
double *begin();
};

void bar(const FOO &foo) {
foo.begin();
}

gives the compilation errors

foo.cc(7): error: the object has cv-qualifiers that are not compatible
with the member function
object type is: const FOO
foo.begin();
^
You can't call non-const member functions with a const object. After all
they are non-const because they modify the object, and you can't modify
a const object.

with Intel icc 7.1 and

foo.cc: In function `void bar(const FOO&)':
foo.cc:7: error: passing `const FOO' as `this' argument of `double*
FOO::begin()' discards qualifiers

with GNU C++ 3.3.1. I cannot figure out what either means. The errors
go away when I remove the "const" keyword. I've obviously mangled the
advice on page 146 of Stroustroup, but don't understand how.


Removing the const makes foo non-const and hence non-const member function
can be called.

The other option is to make the member function const:

class FOO {
public:
double *begin() const;
};

But that only works if begin() doesn't actually modify anything in the
object (or do things like return a non-const reference to a member
of the object).

--
Sam Holden

Jul 19 '05 #4

"Jim West" <eg***********@yahoo.com> wrote in message
news:sl**************************@jwest.ecen.oksta te.edu...

Could someone please explain to me why the code segment

class FOO {
public:
double *begin();
};

void bar(const FOO &foo) {
foo.begin();
}

gives the compilation errors
I've obviously mangled the
advice on page 146 of Stroustroup, but don't understand how.


If you are looking at p. 146 of the 3rd edition, then you've got it partly
right. But your case is more complicated than any he shows - note that he
hasn't introduced classes yet on p. 146! You need to look on p. 229-30,
where he talks about const member functions. Specifically, the line
cd.add_year(1); // error; cannot change value of const cd

In short, your begin() function does not promise not to change the value of
foo.
Jul 19 '05 #5
In article <sl********************@flexal.cs.usyd.edu.au>, Sam Holden wrote:

The other option is to make the member function const:

class FOO {
public:
double *begin() const;
};

But that only works if begin() doesn't actually modify anything in the
object (or do things like return a non-const reference to a member
of the object).
Thanks to all who gave the answer so quickly. begin() doesn't change anything
in foo, so it should be safe. I'll study Stroustrup pp. 229-30 as recommended
by the jeffc tomorrow when I get access to it again.

Jul 19 '05 #6

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

Similar topics

1
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
11
by: Der Andere | last post by:
What exactly is the sense of using a "const" modifier before a function declaration? Actually, you can use it in three places. For instance, take a look at the following function declaration (from...
1
by: Michael Fitzpatrick | last post by:
Is it possible to set a variable with the value of a compile time constant. Example: #Const MODE_STR = "Mode1" dim strMode as String = MODE_STR Obviously this doesn't work. Is there a way...
9
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
6
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
3
by: Jess | last post by:
Hello, If I have a constant array, i.e. it's elements aren't changed, should I declare it as: const int a = {1,2,3}; or int const a = {1,2,3}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.