473,383 Members | 1,952 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,383 software developers and data experts.

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_others_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_others_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 1780
"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_others_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_others_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_others_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_others_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
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...
6
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...
5
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...
14
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...
11
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
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...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
4
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 =...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.