Newbie question: reference as argument

Discussion in 'C++' started by Manuel, Jan 13, 2006.

  1. Manuel

    Manuel Guest

    Hi.

    I've a code like this:
    ------------------------------------
    pngInfo textInfo;

    texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    GL_CLAMP, GL_NEAREST, GL_LINEAR);
    ------------------------------------

    where pngBind need a reference to textInfo.

    Now I want include this code into a function that take as argument a
    reference to textInfo. I'm not sure about how to write this function.

    1)

    void myFunction( pngInfo &textInfo)
    {
    texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    GL_CLAMP, GL_NEAREST, GL_LINEAR);
    }

    or

    2)

    void myFunction( pngInfo &textInfo)
    {
    texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, texInfo, GL_CLAMP,
    GL_NEAREST, GL_LINEAR);
    }

    In case 1 it seem a sort of reference of reference...in case 2...not
    sure what I've written...

    Regards,

    Manuel
    Manuel, Jan 13, 2006
    #1
    1. Advertising

  2. Manuel

    Guest

    Manuel wrote:
    > Hi.
    >
    > I've a code like this:
    > ------------------------------------
    > pngInfo textInfo;
    >
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    > GL_CLAMP, GL_NEAREST, GL_LINEAR);


    This is invalid unless the 4th argument expected is a pointer, not a
    reference. There is some inconsistency in your post....

    > ------------------------------------
    >
    > where pngBind need a reference to textInfo.
    >
    > Now I want include this code into a function that take as argument a
    > reference to textInfo. I'm not sure about how to write this function.
    >
    > 1)
    >
    > void myFunction( pngInfo &textInfo)
    > {
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    > GL_CLAMP, GL_NEAREST, GL_LINEAR);


    Use this if you need a pointer to a pngInfo.

    > }
    >
    > or
    >
    > 2)
    >
    > void myFunction( pngInfo &textInfo)
    > {
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, texInfo, GL_CLAMP,
    > GL_NEAREST, GL_LINEAR);


    Use this one if the expected type is a pngInfo either by value or
    reference.
    > }
    >
    > In case 1 it seem a sort of reference of reference...


    An address of a reference, which is really the address of the actual
    value, not the reference...

    in case 2...not
    > sure what I've written...
    >
    > Regards,
    >
    > Manuel
    , Jan 13, 2006
    #2
    1. Advertising

  3. Manuel wrote:
    > I've a code like this:
    > ------------------------------------
    > pngInfo textInfo;
    >
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    > GL_CLAMP, GL_NEAREST, GL_LINEAR);
    > ------------------------------------
    >
    > where pngBind need a reference to textInfo.


    If 'pngBind' needs a reference, you should drop the '&'.

    > Now I want include this code into a function that take as argument a
    > reference to textInfo. I'm not sure about how to write this function.
    >
    > 1)
    >
    > void myFunction( pngInfo &textInfo)
    > {
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    > GL_CLAMP, GL_NEAREST, GL_LINEAR);


    Drop the '&', I am telling you. You're trying to take address of
    'texInfo' (BTW, do you know that you've misspelled 'textInfo'?)

    > }
    >
    > or
    >
    > 2)
    >
    > void myFunction( pngInfo &textInfo)
    > {
    > texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, texInfo, GL_CLAMP,
    > GL_NEAREST, GL_LINEAR);


    That's it. Now spell it correctly.

    > }
    >
    > In case 1 it seem a sort of reference of reference...in case 2...not
    > sure what I've written...


    When a reference is initialised from a reference, it refers to the same
    object at the one from which it's initialised. It's OK, trust me.

    V
    Victor Bazarov, Jan 13, 2006
    #3
  4. On Sat, 14 Jan 2006 00:14:46 +0100, Manuel
    <> wrote:

    >Hi.
    >
    >I've a code like this:
    >------------------------------------
    >pngInfo textInfo;
    >
    >texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    >GL_CLAMP, GL_NEAREST, GL_LINEAR);
    >------------------------------------
    >
    >where pngBind need a reference to textInfo.


    Assuming that "&texInfo" is a misspelling for "&textInfo", you are
    passing the *address* of your variable textInfo to the function. If
    pngBind really expects a reference and not a pointer, this shouldn't
    compile. Instead, you need to just remove the "&".

    >Now I want include this code into a function that take as argument a
    >reference to textInfo. I'm not sure about how to write this function.
    >
    >1)
    >
    >void myFunction( pngInfo &textInfo)
    >{
    >texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &texInfo,
    >GL_CLAMP, GL_NEAREST, GL_LINEAR);
    >}
    >
    >or
    >
    >2)
    >
    >void myFunction( pngInfo &textInfo)
    >{
    >texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, texInfo, GL_CLAMP,
    >GL_NEAREST, GL_LINEAR);
    >}
    >
    >In case 1 it seem a sort of reference of reference...in case 2...not
    >sure what I've written...


    It's probably No. 2 (see comments above), except that you seem to have
    misspelled the name. It all depends on whether the declaration of
    pngBind says "pngInfo&" or "pngInfo*".

    --
    Bob Hairgrove
    Bob Hairgrove, Jan 13, 2006
    #4
  5. Manuel

    Manuel Guest

    Bob Hairgrove wrote:

    > It all depends on whether the declaration of
    > pngBind says "pngInfo&" or "pngInfo*".


    Thanks!
    Oops...pngBind get a pointer:
    http://www.fifi.org/doc/libglpng-dev/glpng.html#pngBind

    "Pointer to a pngInfo structure to store texture info or NULL if you
    don't care"

    So, the correct way is:

    void myFunction( pngInfo &textInfo)
    {
    texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &textInfo,
    GL_CLAMP, GL_NEAREST, GL_LINEAR);
    }

    ok?
    Manuel, Jan 13, 2006
    #5
  6. On Sat, 14 Jan 2006 00:43:51 +0100, Manuel
    <> wrote:

    >Bob Hairgrove wrote:
    >
    >> It all depends on whether the declaration of
    >> pngBind says "pngInfo&" or "pngInfo*".

    >
    >Thanks!
    >Oops...pngBind get a pointer:
    >http://www.fifi.org/doc/libglpng-dev/glpng.html#pngBind
    >
    >"Pointer to a pngInfo structure to store texture info or NULL if you
    >don't care"
    >
    >So, the correct way is:
    >
    >void myFunction( pngInfo &textInfo)
    >{
    >texture = pngBind("foo.png", PNG_NOMIPMAP, PNG_ALPHA, &textInfo,
    >GL_CLAMP, GL_NEAREST, GL_LINEAR);
    >}
    >
    >ok?


    Looks OK to me. You are passing textInfo by reference to myFunction(),
    but you pass the pointer to pngBind(), so it should work. Just make
    sure that you call myFunction() correctly now.

    It's probably unfortunate (for newbies) that the syntax for the
    address-of operator and the declaration of a reference argument are
    the same. But you get used to it after a while.

    --
    Bob Hairgrove
    Bob Hairgrove, Jan 14, 2006
    #6
  7. Manuel

    Manuel Guest

    Bob Hairgrove wrote:

    > Looks OK to me. You are passing textInfo by reference to myFunction(),
    > but you pass the pointer to pngBind(), so it should work. Just make
    > sure that you call myFunction() correctly now.
    >


    Thx!

    > It's probably unfortunate (for newbies) that the syntax for the
    > address-of operator and the declaration of a reference argument are
    > the same. But you get used to it after a while.
    >
    >


    Yes, some sintax are very hard...
    fortunately there is this NG :)

    Best regards,

    Manuel
    Manuel, Jan 14, 2006
    #7
    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. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    411
  2. Ben Kial
    Replies:
    1
    Views:
    648
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,883
    Eric Sosman
    Jan 7, 2005
  4. mrstephengross
    Replies:
    2
    Views:
    403
    mrstephengross
    Sep 7, 2005
  5. nw
    Replies:
    0
    Views:
    312
Loading...

Share This Page