From: t.wanat on

I'm trying to slowly convert some C code to C++ and have encounter
what's probably a trivial problem.

The easiest way to describe the problem is with an example:

class FOO
{
public:
int Version(void){return m_ver;};
FOO(){m_ver = 1;};
private:
int m_ver;
};

void PrintVer(const FOO * fp)
{
int v = fp->Version(); /* error here */

cout << "The version is " << v << endl;
}

When I try to compile, I get the error
'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO
&'

on the line containing fp->Version().

If I remove the const from the routine declaration, it works fine, but
I'd rather not do that. I guess I don't understand what the compiler
is trying to tell me, and have no idea of how to work around this
without removing the const.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
t.wanat(a)nci-sw.com wrote:
> class FOO
> {
> public:
> int Version(void){return m_ver;};
> FOO(){m_ver = 1;};
> private:
> int m_ver;
> };
>
> int v = fp->Version(); /* error here */
>
> When I try to compile, I get the error
> 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO
> &'
>
> on the line containing fp->Version().
>
> If I remove the const from the routine declaration, it works fine, but
> I'd rather not do that. I guess I don't understand what the compiler
> is trying to tell me, and have no idea of how to work around this
> without removing the const.

The compiler is trying to tell you that PrintVer() promises never to modify
the passed FOO object, but Version() doesn't do so. I guess that version
needs a const, too. Please also see the FAQ and any good C++ book for
explanations what 'const correctness' in C++ means.

Uli


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: paradox on
You are calling a non-const member function on a const object! Since
you are passing a const Foo as a parameter and then calling fp-
>Version(), fp is const but Version is not const so the compiler
thinks you are modifying a class member inside the function. You
should make your Version member function const. To do this just place
const after the function declaration. This tells the compiler that you
aren't changing any class members inside that function so now you are
able to call it on a const Foo object! I hope this cleared things up!

class FOO
{
public:
int Version(void) const { return m_ver; };
FOO(){m_ver = 1;};
private:
int m_ver;
};

void PrintVer(const FOO * fp)
{
int v = fp->Version(); /* error here */

cout << "The version is " << v << endl;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Friedhelm Hoerner on

Any member function which *may* alter the object cannot be called with
a const object - otherwise constness is violated.
You should tell the compiler that Version doesn' modify FOO:

int Version(void) const {return m_ver;};
--------------------------^

In your example the compiler could determine the constness, but if
there is only a declaration in the header:

int Version(void);

and the definition is in another source.cpp

int FOO::Version(void)
{
return m_ver;
}

it is not possible to check constness.

Declaring the member function "const" will give you an Error if you
try to change the object:

int Version(void) const {return m_ver++;};
- Error here ------------------------------^

therefore declaring member functions const if they may not alter the
object is always a good idea.

F.Hoerner

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: kasthurirangan.balaji on
On Apr 18, 6:33 am, t.wa...(a)nci-sw.com wrote:
> I'm trying to slowly convert some C code to C++ and have encounter
> what's probably a trivial problem.
>
> The easiest way to describe the problem is with an example:
>
> class FOO
> {
> public:
> int Version(void){return m_ver;};
> FOO(){m_ver = 1;};
> private:
> int m_ver;
> };
>
> void PrintVer(const FOO * fp)
> {
> int v = fp->Version(); /* error here */
>
> cout << "The version is " << v << endl;
> }
>
> When I try to compile, I get the error
> 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO
> &'
>
> on the line containing fp->Version().
>
> If I remove the const from the routine declaration, it works fine, but
> I'd rather not do that. I guess I don't understand what the compiler
> is trying to tell me, and have no idea of how to work around this
> without removing the const.

{ edits: quoted banner (see the end of this article) removed, please don't quote
extraneous material -- even if the clc++m banner is a very fine one. -mod }

since you do not want to remove const off the parameter, your Version
member function should be
int Version(void) const {return m_ver;}; <-- this ; is not required

the compiler is reporting you about the const correctness.

Thanks,
Balaji.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]