473,789 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const pointer / reference nonsense (to me, anyway :-)

Hi,

Could anyone explain to me why this works? I can't figure out
the wonky const syntax!

class Surface
{
...

protected:
SDL_Surface* data;

static SDL_Surface* const&
get_others_data (const Surface& arg);
...
};

SDL_Surface* const&
Surface::get_ot hers_data(const Surface& arg)
{
return arg.data;
}

As you can see, it returns a reference to a pointer, so the
pointer itself can be manipulated/reset (why? If you can to
know, I can tell you later.)

When I declared it as 'const SDL_Surface*&', it threw a funny
error, though (below). I can't understand the change in syntax
that was needed.

src/System/Surface.cc: In static member function `static const
SDL_Surface*&
Shotgun_Robot:: Surface::get_ot hers_data(const
Shotgun_Robot:: Surface&)':
src/System/Surface.cc:275: error: invalid initialization of
reference of type '
const SDL_Surface*&' from expression of type 'SDL_Surface* const'

Thanks,
Asfand Yar

--
http://www.it-is-truth.org/
Jul 22 '05 #1
3 1795
"Asfand Yar Qazi" <im_not_giving_ it_here@i_hate_ spam.com> wrote...
Could anyone explain to me why this works? I can't figure out
the wonky const syntax!

class Surface
{
...

protected:
SDL_Surface* data;

static SDL_Surface* const&
get_others_data (const Surface& arg);
...
};

SDL_Surface* const&
Surface::get_ot hers_data(const Surface& arg)
{
return arg.data;
}

As you can see, it returns a reference to a pointer, so the
pointer itself can be manipulated/reset (why? If you can to
know, I can tell you later.)
No, the pointer returned is 'const'. Its value cannot be changed.
The value of the object it points to, can, however.
When I declared it as 'const SDL_Surface*&', it threw a funny
error, though (below). I can't understand the change in syntax
that was needed.
When you put 'const' between SDL_Surface and '*' (and that's
where it really goes even if you put it first), you declare
the pointer non-const but the object SDL_Surface const.

src/System/Surface.cc: In static member function `static const
SDL_Surface*&
Shotgun_Robot:: Surface::get_ot hers_data(const
Shotgun_Robot:: Surface&)':
src/System/Surface.cc:275: error: invalid initialization of
reference of type '
const SDL_Surface*&' from expression of type 'SDL_Surface* const'


You attempt to return a non-const reference to a member of
an object that was declared const. 'arg' designates a const
object of type Surface. You cannot initialise a non-const
reference to any of its members.

Victor
Jul 22 '05 #2
Asfand Yar Qazi wrote:
Hi,

Could anyone explain to me why this works? I can't figure out
the wonky const syntax!

class Surface
{
...

protected:
SDL_Surface* data;

static SDL_Surface* const&
get_others_data (const Surface& arg);
...
};

SDL_Surface* const&
Surface::get_ot hers_data(const Surface& arg)
{
return arg.data;
}

As you can see, it returns a reference to a pointer, so the
pointer itself can be manipulated/reset (why? If you can to
know, I can tell you later.)
Yes.. I agree this is a funny feature of C++ since its true you are able to
return pointers and references to private data members. This feature makes
much more easy to code things like operator[] that way you are able to
return a l-value.

But I agree it is very odd at first glance..

src/System/Surface.cc: In static member function `static const
SDL_Surface*&
Shotgun_Robot:: Surface::get_ot hers_data(const
Shotgun_Robot:: Surface&)':
src/System/Surface.cc:275: error: invalid initialization of
reference of type '
const SDL_Surface*&' from expression of type 'SDL_Surface* const'

That is for the return type and the actual type you are returning dont
match. You have to return exactly the same type, you may do a cast and it
will work too.
Regards

Roberto
Jul 22 '05 #3

int main(void)
{

int tree = 7;

int* pGrass = &tree;

int* &pFlowers = pGrass;
if (pFlowers == pGrass)
{
std::cout << "Test 1: Equal values, PASSED";
}
if (&pFlowers == &pGrass)
{
std::cout << "Test 2: Equal addresses, PASSED";
}
}

Now, pFlowers is just another name for pGrass. Whatever you do to

Jul 22 '05 #4

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

Similar topics

2
16247
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
6
2424
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
5
3756
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
14
2544
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over pointers in some cases... TIA
11
2379
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
17
2668
by: codeslinger | last post by:
What is the point of the construct in the subject line? Why the use of two const labels? Does that do something different than const void *x? And what would void * const x indicate? Thanks in advance :) -- Tobias DiPasquale
51
4476
by: Kuku | last post by:
What is the difference between a reference and a pointer?
4
6697
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
3
4791
by: Hank stalica | last post by:
So I have this class with a private data member: const Floor* const destFloor; I need to initialize f, which I'm trying to do in the constructor initializer list: Rider::Rider(const Floor& f) :destFloor(f)
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.