473,698 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16954
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. okstate.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
2500
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
4756
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 the introductory book "Absolute C++" by W. Savitch, p. 315) class Money { Money(); const Money operator + (const Money& amount2) const; private:
1
3117
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 to achieve the same thing???
9
3623
by: July | last post by:
Hello! consider the following code: class A { public: virtual void f() const{ cout << "A::f()" << endl; } };
6
8793
by: denis | last post by:
What are the benefits of using const functions? How does the compiler interpret const functions? Thanks, Denis
4
6082
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 definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
16
2108
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
26
11718
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
3
23607
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
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7734
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.