Passing References: Is this correct and according to standard?

Discussion in 'C++' started by Akshay Loke, Jul 10, 2008.

  1. Akshay Loke

    Akshay Loke Guest

    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
    Akshay Loke, Jul 10, 2008
    #1
    1. Advertising

  2. Akshay Loke

    James Kanze Guest

    On Jul 11, 1:10 am, "Alf P. Steinbach" <> wrote:
    > * Akshay Loke:
    > > 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?


    > Probably it doesn't return a temporary const object. Probably
    > it returns an object. Which is not const but is an rvalue.


    > > 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!


    > 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?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 11, 2008
    #2
    1. Advertising

  3. Akshay Loke

    Akshay Loke Guest

    On Jul 10, 8:31 pm, "Daniel T." <> wrote:

    > 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
    Akshay Loke, Jul 11, 2008
    #3
  4. Akshay Loke

    Akshay Loke Guest

    On Jul 11, 4:02 am, James Kanze <> wrote:
    > On Jul 11, 1:10 am, "Alf P. Steinbach" <> wrote:
    >
    >
    >
    >
    >
    > > * Akshay Loke:
    > > > 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?

    > > Probably it doesn't return a temporary const object. Probably
    > > it returns an object. Which is not const but is an rvalue.
    > > > 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!

    > > 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?
    >
    > --
    > - Show quoted text -


    I can't change the function dclaration since it is a built-in Maya API
    function..
    Akshay Loke, Jul 11, 2008
    #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. Replies:
    8
    Views:
    339
  2. s.subbarayan

    Is this correct according to C coding standards?

    s.subbarayan, Mar 8, 2005, in forum: C Programming
    Replies:
    8
    Views:
    529
    Keith Thompson
    Mar 10, 2005
  3. Denis
    Replies:
    0
    Views:
    299
    Denis
    Apr 25, 2007
  4. speedman2010
    Replies:
    2
    Views:
    609
    Roedy Green
    May 10, 2008
  5. Kasper Tidemann
    Replies:
    1
    Views:
    340
    Rick DeNatale
    Apr 29, 2010
Loading...

Share This Page