Newbie question: reference as argument

M

Manuel

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
 
R

roberts.noah

Manuel said:
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
 
V

Victor Bazarov

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

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

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
 
B

Bob Hairgrove

Hi.

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

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

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*".
 
M

Manuel

Bob said:
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?
 
B

Bob Hairgrove

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

Manuel

Bob said:
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
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top