Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Asfand Yar Qazi
Guest
 
Posts: n/a
#1: Jul 22 '05
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/

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

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


"Asfand Yar Qazi" <im_not_giving_it_here@i_hate_spam.com> wrote...[color=blue]
> 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.)[/color]

No, the pointer returned is 'const'. Its value cannot be changed.
The value of the object it points to, can, however.
[color=blue]
> 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.[/color]

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.
[color=blue]
>
> 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'[/color]

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


Roberto Díaz
Guest
 
Posts: n/a
#3: Jul 22 '05

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


Asfand Yar Qazi wrote:
[color=blue]
> 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.)[/color]

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

[color=blue]
> 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'[/color]


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
JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

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



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

Closed Thread