C/C++: Returning References?

Community Forums/General Help/C/C++: Returning References?

Gabriel(Posted 2009) [#1]
I've been having a look at Ogre, possibly with a view to wrapping it for BlitzMax, but I'm finding it very much designed for C++ and kinda hard to make a C wrapper for. Aside from templates and returning UDT's by value, I've found this function declaration.

const PluginInstanceList& Ogre::Root::getInstalledPlugins ( ) const


Two things immediately strike me about this.

1) I have no idea what returning a reference is all about. I don't know what it does or why. A pointer to a PluginInstanceList would make sense, but returning a reference, nope, don't get it. I can't find it in my books either, although it's probably there somewhere.

2) How do I give that a C wrapper?

My first instinct is to do this:

OGREEXPORT const PluginInstanceList& OgreRoot_GetInstalledPlugins(Root* Ha) {
	return Ha->getInstalledPlugins();
}


But that tells me I'm missing a type specifier. Possibly because of the const in the return? If I remove the const, I get a syntax error before the & character, so it doesn't like that either.


markcw(Posted 2009) [#2]
The '&' is the reference operator and can be read as 'address of variable' whereas the '*' is the dereference operator and can read as 'value pointed by variable'.

This pointers tutorial should clarify and this references tutorial is also worth a read.

Consts in C++ are really confusing so you should look it up if you're not sure. There was a good const tutorial I read but can't seem to find it now. Anyway, here's a good const tutorial on wikipedia and another on:
http://www.parashift.com/c++-faq-lite/const-correctness.html


Gabriel(Posted 2009) [#3]
I understand what references and pointers are, I just don't understand returning a reference, conceptually. It seems like you would end up with a ghastly syntax in order to use it.

It would be something like:

DoSomethingWithA(A)=B;

Wouldn't it? I'd have hated C++ even more if I'd known this was possible.

I think my mistake in wrapping the function comes down to a scope problem. I assumed that PluginInstanceList was a class of it's own, but it appears to be a vector of plugins within the Root class, so I needed to add Root:: in the function prototype.

So I think I'll either have to wrap vectors as well, or just create an array of the plugins C-side and return a pointer to it.


Brucey(Posted 2009) [#4]
Returning references are fun in BlitzMax... until you actually return it, and it goes out of scope :-)


Brucey(Posted 2009) [#5]
For references on references, see wxBitmap, wxColour, wxBrush, wxPen etc in wxMax.

Well, it's one way around the issue, and works for me.

On a different note, all wxEvents are actually return using their reference, rather than a pointer. I can do that, because I know the event will remain in scope for the duration that it is used. (i.e. directly after being raised by the system). Once the event callback returns though, the memory for the event will be undefined, having gone out of scope in the library.

Fun fun.

And of course, different libraries work in different ways, which just adds to the overall general fun-ness :-)