Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: const point...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 13 of 24 Topic 9518 of 9828
Post > Topic >>

Re: const pointer to class problem

by Lionel B <me@[EMAIL PROTECTED] > Apr 18, 2008 at 06:13 AM

On Thu, 17 Apr 2008 19:33:58 -0600, t.wanat 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;};
       //           ^                  ^
       // drop the "void"          extraneous ";"

       int Version() const {return m_ver;}
       //              ^
       //         add this

>     FOO(){m_ver = 1;};
       //               ^
       //         extraneous ";"
> 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().

This is because "const FOO * fp" declares a pointer to a const FOO
object; so *fp cannot be modified in the function PrintVer(). But the
function FOO::Version() does not guarantee that the FOO object it is
invoked on will not be modified, so the compiler complains. You can
guarantee that FOO::Version() will not modify its invoking object by
declaring it "const" (see above).

Note that declaring the parameter to PrintVer() as "FOO* const fp" would
also solve your problem; it says that the *pointer* fp is const (rather
than the object it points to). It depends what you want, really... if the
function FOO::Version() really does not modify its invoking object it
should be declared const; and if PrintVer() does not modify the pointer
fp *or* the object it points to (as I suspect is your case), then the
parameter should probably be declared as "const FOO* const fp".

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

Hope this helps,

-- 
Lionel B

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




 24 Posts in Topic:
const pointer to class problem
t.wanat@[EMAIL PROTECTED]  2008-04-17 19:33:58 
Re: const pointer to class problem
Ulrich Eckhardt <dooms  2008-04-18 05:05:12 
Re: const pointer to class problem
paradox <ksjetski@[EMA  2008-04-18 06:13:16 
Re: const pointer to class problem
Friedhelm Hoerner <f.h  2008-04-18 06:13:16 
Re: const pointer to class problem
kasthurirangan.balaji@[EM  2008-04-18 06:13:17 
Re: const pointer to class problem
Tony Delroy <tony_in_d  2008-04-18 06:13:15 
Re: const pointer to class problem
Chris Uzdavinis <cuzda  2008-04-18 06:13:17 
Re: const pointer to class problem
Christopher <cpisz@[EM  2008-04-18 06:13:18 
Re: const pointer to class problem
Oncaphillis <oncaphill  2008-04-18 06:13:15 
Re: const pointer to class problem
red floyd <no.spam@[EM  2008-04-18 06:13:16 
Re: const pointer to class problem
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-18 06:13:19 
Re: const pointer to class problem
"Thomas J. Gritzan&q  2008-04-18 06:13:17 
Re: const pointer to class problem
Lionel B <me@[EMAIL PR  2008-04-18 06:13:18 
Re: const pointer to class problem
Gerhard Menzl <clcppm-  2008-04-18 06:13:17 
Re: const pointer to class problem
Thomas Lehmann <t.lehm  2008-04-18 17:16:36 
Re: const pointer to class problem
David Pol <david@[EMAI  2008-04-18 17:15:18 
Re: const pointer to class problem
rwf_20 <rfrenz@[EMAIL   2008-04-18 17:17:39 
Re: const pointer to class problem
Mathias Gaunard <loufo  2008-04-18 17:18:25 
Re: const pointer to class problem
Todd Mars <tamnt54@[EM  2008-04-18 17:18:24 
Re: const pointer to class problem
SeanW <sean_woolcock@[  2008-04-18 17:17:37 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 15:47:59 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 15:51:21 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 21:01:04 
Re: const pointer to class problem
Ulrich Eckhardt <dooms  2008-04-21 10:25:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:36:24 CDT 2008.