473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Need help understanding a const function returning an object reference.

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?

Sep 21 '07 #1
5 1618
no***********@gmail.com 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
Sep 21 '07 #2
no***********@gmail.com 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'.
Sep 21 '07 #3
On Sep 21, 2:14 pm, "Daniel T." <danie...@earthlink.netwrote:
noobzillak...@gmail.com 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.

Sep 21 '07 #4
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.
Sep 21 '07 #5
On 21 Sep, 10:21, Juha Nieminen <nos...@thanks.invalidwrote:
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.

Sep 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
6
by: michaelkatsilis | last post by:
Hi, Are there any issues with returning a const reference value for public "get" accessor methods? eg. const string & getVal(); // or const MyString & getVal();
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
7
by: pmatos | last post by:
Hi all, I've been having questions about strings, references, initializations... I've created code (which will not compile due to a reference problem) and I'd like comments on why this won't...
1
by: maadhuu | last post by:
what is the use of returning "const & "from a function in C++ ?? Is it used somewhere at all ?? thank you, Ranjan.
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
12
by: Luca | last post by:
I have 2 questions: 1. Which one is the best: Returning member variables of the class by const ref or returning by pointer? e.g.: Employee* GetEmployee() const; Employee& ...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.