Connecting Tech Pros Worldwide Help | Site Map

Using "const" : why does compile fail

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:11 PM
Jim West
Guest
 
Posts: n/a
Default 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.



  #2  
Old July 19th, 2005, 07:11 PM
WW
Guest
 
Posts: n/a
Default 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


  #3  
Old July 19th, 2005, 07:11 PM
lilburne
Guest
 
Posts: n/a
Default 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.

  #4  
Old July 19th, 2005, 07:11 PM
Sam Holden
Guest
 
Posts: n/a
Default 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

  #5  
Old July 19th, 2005, 07:11 PM
jeffc
Guest
 
Posts: n/a
Default 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.


  #6  
Old July 19th, 2005, 07:11 PM
Jim West
Guest
 
Posts: n/a
Default 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]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.