Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conversion loses qualifiers

Reply
Thread Tools

Conversion loses qualifiers

 
 
David Kevin
Guest
Posts: n/a
 
      05-02-2012
Hi,
I would be grateful if somebody could me help with following piece of
code:

#include "stdafx.h"
#include <iostream>
using namespace std;

class MyClass {
const void func() {}
const void func2(const MyClass* myClass) {
myClass->func();
}
};

int main()
{
return 0;
}

My compiler (Visual C++ 200 complains that there is an error at line
8:
..\Problem.cpp( : error C2662: 'MyClass::func' : cannot convert
'this' pointer from 'const MyClass' to 'MyClass &'
Conversion loses qualifiers.

I would understand that if func could change myClass. But it is
declared as const. What happens?

Thanks in advance for responses,
Greetings.
 
Reply With Quote
 
 
 
 
David Kevin
Guest
Posts: n/a
 
      05-02-2012
On 2 Maj, 11:41, brunobeltr...@gmail.com wrote:
> My compiler says:
> g++ test.cpp
> test.cpp: In member function ‘const void MyClass::func2(const MyClass*)’:
> test.cpp:7: error: no matching function for call to ‘MyClass::func() const’
> test.cpp:5: note: candidates are: const void MyClass::func() <near match>
>
> Suggesting that perhaps the thing to change is the
> const void func() {}
> to
> void func() const {}
>
> That's the convention I've always seen, and although I am an extreme novice with regards to C++, experience with the complex semantics involved in parsing C function pointers suggests that it is not unlikely that C++'s grammar requires specific placement of the const qualifier to distinguish a function that returns, say, a constant int from a function which returns a mutable int and leaves the class constant.
> This technicality would then undoubtedly carry over into functions that return a void (even though there is no such thing as the concept of a constant nothing) due do a desire for consistency.
> That's just my guess.


Thank you very much. It is what really works. Now I have recalled that
I have read something similiar to that what you have just said in
regular C++ handbook. But it's something strange that I couldn't see
it in Bjarne Stroustrups' "C++ Programming Language" book to which I
have referenced (it wasn't possible to use that function modifier in
older standard?). On the other hand, I haven't reviewed that in enough
details to be sure that it isn't there.

 
Reply With Quote
 
 
 
 
Kevin McCarty
Guest
Posts: n/a
 
      05-02-2012
On May 2, 3:13*am, David Kevin <davidke...@o2.pl> wrote:
> On 2 Maj, 11:41, brunobeltr...@gmail.com wrote:
>
> > My compiler says:
> > g++ test.cpp
> > test.cpp: In member function ‘const void MyClass::func2(const MyClass*)’:
> > test.cpp:7: error: no matching function for call to ‘MyClass::func() const’
> > test.cpp:5: note: candidates are: const void MyClass::func() <near match>

>
> > Suggesting that perhaps the thing to change is the
> > const void func() {}
> > to
> > void func() const {}


Yes. In the context of a member function declaration, the former is a
non-const member function that returns void (well, const void, which
in practice is absolutely the same thing). The latter is a const
member function that returns void. The 'const' after the argument
list is a promise that the function does not affect the state of the
object with which it is called. (Neglecting any use of mutable or
const_cast keywords, C-style casting to non-const, or other peripheral
issues.)


> Thank you very much. It is what really works. Now I have recalled that
> I have read something similiar to that what you have just said in
> regular C++ handbook. But it's something strange that I couldn't see
> it in Bjarne Stroustrups' "C++ Programming Language" book to which I
> have referenced (it wasn't possible to use that function modifier in
> older standard?). On the other hand, I haven't reviewed that in enough
> details to be sure that it isn't there.


TC++PL section 10.2.6 (in the 3rd edition) -- "Constant Member
Functions".

Hope this helps,
- Kevin B. McCarty
 
Reply With Quote
 
David Kevin
Guest
Posts: n/a
 
      05-05-2012
On 2 Maj, 17:39, Kevin McCarty <kmcca...@gmail.com> wrote:
> TC++PL section 10.2.6 (in the 3rd edition) -- "Constant Member
> Functions".
>
> Hope this helps,


Yes, thanks.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      05-08-2012
David Kevin <> wrote:
> Conversion loses qualifiers.


This is compiler speak for "you are trying to use a const object as
non-const". (There might be other situations that trigger the same
message, but attempting to bypass constness without a cast is by far
the most common one.)

When you have a const pointer to an object, you can only call member
functions that are marked as 'const'. (You can call non-const ones by
removing the constness of the pointer with a const_cast, but that's
usually *not* recommended, and should be done only as the utmost last
resort, when no other solution is possible.) Hence the function you
should call should have the 'const' qualifier (or it should have an
overloaded version with that qualifier).

 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-08-2012
Juha Nieminen wrote:

> This is compiler speak for "you are trying to use a const object as
> non-const".



I would rephrase that in a way that would be more helpful to the user, such
as:

"You are trying to call a non-const method of a const object".


I've just filed a bug report for GCC with this suggestion. The bug report
is available at:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53281


Rui Maciel
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      05-08-2012
Rui Maciel <> wrote:
> Juha Nieminen wrote:
>
>> This is compiler speak for "you are trying to use a const object as
>> non-const".

>
>
> I would rephrase that in a way that would be more helpful to the user, such
> as:
>
> "You are trying to call a non-const method of a const object".


I think the same message is given also in other situations where a
const object is attempted to be used as non-const (such as trying to
assign the const pointer to a non-const one, or trying to give it to
a function taking a non-const pointer).
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of tiff file using irfanview loses comments. simon Digital Photography 4 03-20-2007 09:38 PM
g++: 'const' qualifiers cannot be applied to... DanielESFA C++ 17 05-22-2005 12:00 PM
error: passing const ... discards qualifiers danny van elsen C++ 6 04-24-2005 10:34 PM
qualifiers and click in server ASP.Net Dan Bass ASP .Net 2 11-22-2004 03:00 PM
Qualifiers lost compiling STL code Tron Thomas C++ 3 08-09-2004 04:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57