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

  • Thread starter Asfand Yar Qazi
  • Start date
A

Asfand Yar Qazi

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
 
V

Victor Bazarov

Asfand Yar Qazi said:
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
 
R

Roberto =?ISO-8859-15?Q?D=EDaz?=

Asfand said:
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
 
J

JKop

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top