[osg-users] Copy constructor and assignment operator

Robert Osfield robert.osfield at gmail.com
Tue Sep 9 02:31:22 PDT 2008


Hi Up Leng,

The osg::ref_ptr<> is a smart pointer so there won't be issues of
dangling pointers - it'll all be handled for you.  You also don't need
to clone the objects that smart pointer has a reference for as OSG
objects almost all support be shared thanks to reference counting.

Have a look through the documentation on the OSG wiki on the topic of
reference pointers.  The OpenSceneGraph Quick Start Guide also
discusses ref_ptr<>.

Robert.

On Tue, Sep 9, 2008 at 9:32 AM, Yip Leng Wong
<yiplengwong at rocketmail.com> wrote:
> Hi,
>
> As I'm a new user of OpenSceneGraph, pardon me for this simple question:
>
> For the following class:
>
> class Foo
> {
> public:
>     Foo() {}
>    virtual ~Foo() {}
>
> private:
>     osg::ref_ptr<osg::Node> mNode;
> };
>
> since mNode is a pointer, is it a must to provide a copy constructor and
> assignment operator for deep copy to prevent dangling pointer?
>
> If there is a need to provide the copy constructor and assignment operator,
> is it correct as illustrated by the codes below?
>
> Foo(const Foo& other)
> {
>     mNode = other.mNode;
> }
>
> Foo& operator=(const Foo& other)
> {
>     if (this == &other)
>         return *this;
>
>    mNode = new osg::Node;
>     mNode = other.mNode;
>     return *this;
> }
>
> Thanks,
> Yip Leng
> ________________________________
> Try cool new emoticons, skins, plus more space for friends. Download Yahoo!
> Messenger Singapore now.
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>



More information about the osg-users mailing list