From: Tony Delroy on
On Apr 18, 10:33 am, t.wa...(a)nci-sw.com wrote:
> class FOO
> {
> public:
> int Version(void){return m_ver;};
>
> void PrintVer(const FOO * fp)
> {
> 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().

Just tell the compiler the Version function can work on a const
object:

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

Tony

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

From: Chris Uzdavinis on
> class FOO {
> public:
> int Version(void){return m_ver;};
[...]
> };
>
> void PrintVer(const FOO * fp)
> {
> int v = fp->Version(); /* error here */

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

Your subject says "const pointer to class" but it's not. It's a [non-
const] pointer to a const object. To answer your question, since the
object--not the pointer-- is const, you cannot call functions on it
unless they are also "const". (The presumption is that const
functions don't modify the object, so it remains unchanged. Ignore
the "mutable" keyword and casting, and it works out pretty well.)

When you write a member function that does not change the object (e.g.
FOO::Version()), declare the function const. Then it can be called on
a const or non-const object:

class FOO {
public:
int Version() const {return m_ver;}
//...
};

Note the "const" after the parameter list.

--
Chris


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

From: Christopher on
On Apr 17, 8:33 pm, 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 }

You have a constant pointer, but your Version method does not promise
the compiler that it will not alter any member data. Any call made
with a constant pointer must promise not to alter member data. To make
such a promise change to:

class Foo
{
public:
const int Version() const // That const after the param list makes
the promise
{
// Note you are returning by value
// making a copy of the member data, adhering to your promise
return m_ver;
}

Foo()
:
// Initializer list is better form
m_ver(1)
{
}

private:
int m_ver;
};

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

From: Oncaphillis on
t.wanat(a)nci-sw.com wrote:

> 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().
>

You have to tell the compiler explicitly that
the Version() method does not alter the FOO
object since the PrintVer function gets a
const pointer.

Methods which really do notalter the internal
state of an object should always be declared
const like:

<snip>
int Version() const {
....
}

</snip>

Hope that helps

O.


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

From: red floyd on
t.wanat(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;};
int Version() const { return m_ver; }

> FOO(){m_ver = 1;};
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;
> }
> [redacted]

You need to declare Version() as a const method. You can only invoke
const methods on const objects.

1. Remove the (void) parameter. That's a C-ism, and frowned on (at
least here).
2. Remove the superflous semicolons after the inline functions.

See my inserts.

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