Using "const" : why does compile fail

J

Jim West

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

lilburne

Jim said:
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.
 
W

WW

Jim said:
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.
 
S

Sam Holden

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).
 
J

jeffc

Jim West said:
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.
 
J

Jim West

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top