Passing References: Is this correct and according to standard?

A

Akshay Loke

Hi all,

I have this function from a class MFnDagNode,
addChild( MObject & child, unsigned int index = kNextPos, bool
keepExistingParents = false );
which takes an object reference as first input parameter (ignore the
rest since those are default)

now in another class, I have:

MFnDagNode parent;
parent.addChild(node.object());
Where node is MfnDependencyNode&
And node.object() returns MObject

This gives me error
‘error: no matching function for call to
‘MfnDagNode::addChild(Mobject)’
Note: candidates are: Mstatus MFnDagNode::addChild(MObject&, unsigned
int, bool)

Now I am not sure why this doesn’t work and the object doesn’t get
implicitly cast to the MObject& reference argument, but I am assuming
that could be because the node.object() returns a temporary const
object and it cannot be cast to a MObject& reference…

So this is the way I have done this…

const MObject& nodeObjectConstRef = node.object();
MObject& nodeObjectRef = const_cast<MObject&> (nodeObjectConstRef);
parent.addChild(nodeObjectRef);

and This works!

Initially I tried this,
MObject& nodeObjectConstRef = node.object();
parent.addChild(nodeObjectConstRef );

but that didn’t work … it gives the error,
‘error: invalid initialization of non-const reference of type
‘MObject&’ from a temporary of type ‘MObject’’

The first method above is working, but am not sure if I am suppressing
something using the const_cast…


Thanks
Akshay
 
J

James Kanze

* Akshay Loke:
Probably it doesn't return a temporary const object. Probably
it returns an object. Which is not const but is an rvalue.
Assuming node.object() returns by value, it's formally UB.

Unless the return type is declared const, I don't see the
undefined behavior. Could you explain?
Why don't you do
MObject nodeObject = node.object();
parent.addChild( nodeObject );

More to the point, why doesn't he declare the function to take a
reference to const?
 
A

Akshay Loke

Better to do this:

MObject obj = node.object(); // makes a modifiable copy of the return
value
addChild( obj );

My guess though is that even this won't really work. I suspect that
'addChild' keeps a pointer to the object passed in and unless you are
very careful, obj is going out of scope before the pointer to it does.- Hide quoted text -

- Show quoted text -

Thanks Alf and Daniel! Creating that modifiable copy of the return
value worked! though i don't know whay i was going the roundabout way
to do this! :)

-Akshay
 
A

Akshay Loke

Unless the return type is declared const, I don't see the
undefined behavior.  Could you explain?


More to the point, why doesn't he declare the function to take a
reference to const?

I can't change the function dclaration since it is a built-in Maya API
function..
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top