Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need help understanding a const function returning an object reference.

Reply
Thread Tools

Need help understanding a const function returning an object reference.

 
 
noobzillaking@gmail.com
Guest
Posts: n/a
 
      09-21-2007
If I have the following code:

struct Pencil
{
int Sharpness;
};
class random
{
public:
Pencil& GetPencil() const {return p;}
Pencil p;
};

Why doesn't this compile.... when.....

struct Pencil
{
int Sharpness;
};
class random
{
public:
Pencil& GetPencil() const {return *(Pencil*)&p;}
Pencil p;
};

Does compile.

Is there something weird about references that I don't know about?

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> If I have the following code:
>
> struct Pencil
> {
> int Sharpness;
> };
> class random
> {
> public:
> Pencil& GetPencil() const {return p;}
> Pencil p;
> };
>
> Why doesn't this compile.... when.....


As inside the const member function, /p/ is typeof "const Pencil",
your return type of "GetPencil" is "Pencil&", "const Pencil" is not
convertible to "Pencil&"

in the latter program, while const member functions are not allowed to
modify any none-mutable member nor to call non-const member functions,
taking the address of a member is not in this scope, type-casting is not
in this scope either.


--
Thanks
Barry
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      09-21-2007
(E-Mail Removed) wrote:

> If I have the following code:
>
> struct Pencil
> {
> int Sharpness;
> };
> class random
> {
> public:
> Pencil& GetPencil() const {return p;}
> Pencil p;
> };
>
> Why doesn't this compile.... when.....


The above doesn't compile because you are exposing a member through a
const function. The member should be exposed as const. As in:

const Pencil& GetPencile() const { return p; }

> struct Pencil
> {
> int Sharpness;
> };
> class random
> {
> public:
> Pencil& GetPencil() const {return *(Pencil*)&p;}
> Pencil p;
> };
>
> Does compile.


The above does compile because you are using a c-style cast to remove
the const from 'p'.
 
Reply With Quote
 
jonyman
Guest
Posts: n/a
 
      09-21-2007
On Sep 21, 2:14 pm, "Daniel T." <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:


>
> The above does compile because you are using a c-style cast to remove
> the const from 'p'.


I hadn't considered that C casts could remove const ness (I thought
const casting (through const_cast) was a new addition to c++).
Regardless: Thank you both for your help.

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-21-2007
jonyman wrote:
> I hadn't considered that C casts could remove const ness (I thought
> const casting (through const_cast) was a new addition to c++).


C-style casting works as static_cast, reinterpret_cast and const_cast
(hence the general guideline to avoid C-style casts, because they are
confusing and can contain hidden surprises). The only new addition in
C++ is dynamic_cast.
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      09-21-2007
On 21 Sep, 10:21, Juha Nieminen <(E-Mail Removed)> wrote:
> jonyman wrote:
> > I hadn't considered that C casts could remove const ness (I thought
> > const casting (through const_cast) was a new addition to c++).

>
> C-style casting works as static_cast, reinterpret_cast and const_cast
> (hence the general guideline to avoid C-style casts, because they are
> confusing and can contain hidden surprises). The only new addition in
> C++ is dynamic_cast.


Generally casts are evil. (See the FAQ).

C++ casts are less evil than C casts because you are less likely to
cast away something you didn't intend.

The only time I normally use a C-style cast is if I'm passing a null
pointer (0) to a function that has more than one overload and I want
to tell the compiler what type of pointer I am passing, so

overloaded_func( (const Foo *) 0 );

which is harmless and really there is no need for

overloaded_func( static_cast< const Foo * >( 0 ) );

which is more confusing because it looks like you are converting
something but in reality you are simply clarifying a literal.

I'm not sure if the new standard is going to bring in a typed null
pointer literal. I know that you'll be able to represent a null
pointer unambiguous from integer 0 but not sure it will be totally
typesafe.

 
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
Const/non-const pointer returning method Jens Thoms Toerring C++ 11 05-26-2010 04:51 PM
Modifying non-const object from const function? fdm C++ 11 09-01-2009 03:14 PM
Re: Modifying non-const object from const function? Francesco C++ 0 08-31-2009 11:30 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments