Need help understanding a const function returning an object reference.

N

noobzillaking

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?
 
B

Barry

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

Daniel T.

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

jonyman

(e-mail address 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.
 
J

Juha Nieminen

jonyman said:
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.
 
E

Earl Purple

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top