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

Discussion in 'C++' started by Asfand Yar Qazi, Jun 6, 2004.

  1. 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/
     
    Asfand Yar Qazi, Jun 6, 2004
    #1
    1. Advertising

  2. "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
     
    Victor Bazarov, Jun 6, 2004
    #2
    1. Advertising

  3. 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
     
    Roberto =?ISO-8859-15?Q?D=EDaz?=, Jun 6, 2004
    #3
  4. Asfand Yar Qazi

    JKop Guest

    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
     
    JKop, Jun 6, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coala
    Replies:
    3
    Views:
    397
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    606
    Victor Bazarov
    Sep 6, 2006
  3. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    10
    Views:
    625
    Pete Becker
    Dec 17, 2007
  5. Disc Magnet
    Replies:
    1
    Views:
    645
    Ian Collins
    May 6, 2010
Loading...

Share This Page