Connecting Tech Pros Worldwide Forums | Help | Site Map

Using "const" : why does compile fail

Jim West
Guest
 
Posts: n/a
#1: Jul 19 '05

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.



WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Using "const" : why does compile fail


Jim West wrote:[color=blue]
> Could someone please explain to me why the code segment
>
> class FOO {
> public:
> double *begin();[/color]

double *begin() const;

You need to tell that the function can be used on const objects.
[color=blue]
> };
>
> 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();
> ^[/color]

--
WW aka Attila


lilburne
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Using "const" : why does compile fail


Jim West wrote:[color=blue]
> 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();
> ^
>
>[/color]

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

Sam Holden
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Using "const" : why does compile fail


On 13 Oct 2003 21:32:59 GMT, Jim West <eggplantparts@yahoo.com> wrote:[color=blue]
>
> 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();
> ^[/color]

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.
[color=blue]
>
> 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.[/color]

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

jeffc
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Using "const" : why does compile fail



"Jim West" <eggplantparts@yahoo.com> wrote in message
news:slrnbom6s1.pvp.eggplantparts@jwest.ecen.oksta te.edu...[color=blue]
>
> 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.[/color]

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.


Jim West
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Using "const" : why does compile fail


In article <slrnbomaqk.v8v.sholden@flexal.cs.usyd.edu.au>, Sam Holden wrote:[color=blue]
>
> 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).[/color]

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.[color=blue]
>[/color]
Closed Thread


Similar C / C++ bytes