Conversion loses qualifiers

D

David Kevin

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++ 2008) complains that there is an error at line
8:
..\Problem.cpp(8) : 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.
 
D

David Kevin

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

Kevin McCarty

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
 
J

Juha Nieminen

David Kevin said:
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).
 
J

Juha Nieminen

Rui Maciel said:
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).
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top