Pointers and reference question

A

Abubakar

Hi,

check the following code:
++++
class nodeinfo{};
nodeinfo * v1_proc1 ()
{
nodeinfo * ni= new nodeinfo();
return ni;
}
void v1_use_proc1()
{
nodeinfo * n = NULL;
n = v1_proc1 ();
// use n....
delete n;

}
++++
another version:

class nodeinfo{};
nodeinfo & v2_proc1 ()
{
nodeinfo * ni= new nodeinfo();
return *ni;
}
void v2_use_proc1()
{
nodeinfo * n = NULL;
n = &v2_proc1 ();
// use n....
delete n;
}
++++

I have checked the code and both versions seem to work fine. My intention
here is that I call proc1() and get a new object of type nodeinfo and start
working with it and delete it when I'm done. Ultimately I want to use this
code concept in a class to offer Clone() functionality. What it would do is
create a new class if its own type and set all the private and public
properties and return a reference or pointer (or whatever!!).

Are these same or they have differences? I mean I can just use first one or
second, and both will give me the same functionality?

Regards,

Ab.
 
T

Tamas Demjen

Abubakar said:
I have checked the code and both versions seem to work fine.

They do, but the reference version is not natural. You're not supposed
to take the address of a reference and call delete on it. A pointer
implies that the object probably needs to be deleted at one moment. You
can't say the same of references.
Ultimately I want to use this
code concept in a class to offer Clone() functionality.

Make Clone a virtual member function of the class, and make it call the
copy constructor:

sturct Base
{
virtual ~Base();
virtual Base* Clone() const = 0;
};

struct D1 : public Base
{
D1();
D1(const D1&);
virtual D1* Clone() const { return new D1(*this); }
};

struct D2 : public Base
{
D2();
D2(const D2&);
virtual D2* Clone() const { return new D2(*this); }
};

I strongly recommend that you consider using boost::shared_ptr and
forget about about native pointers. Not only will you avoid most memory
leaks that way, but you can also say goodbye to double deletion and
pointers to rogue objects (invalid objects, already deleted objects).
Using weak_ptr, you can make sure that when an object dies, all pointers
to that object get automatically NULLed out. IMO, it's the single most
important library feature that radically improves your code quality.

I wrote a small introductory tutorial about shared_ptr:
http://tweakbits.com/articles/sharedptr/index.html

Tom
 
A

Abubakar

Thanks for the code.

Ab.

Tamas Demjen said:
They do, but the reference version is not natural. You're not supposed
to take the address of a reference and call delete on it. A pointer
implies that the object probably needs to be deleted at one moment. You
can't say the same of references.


Make Clone a virtual member function of the class, and make it call the
copy constructor:

sturct Base
{
virtual ~Base();
virtual Base* Clone() const = 0;
};

struct D1 : public Base
{
D1();
D1(const D1&);
virtual D1* Clone() const { return new D1(*this); }
};

struct D2 : public Base
{
D2();
D2(const D2&);
virtual D2* Clone() const { return new D2(*this); }
};

I strongly recommend that you consider using boost::shared_ptr and
forget about about native pointers. Not only will you avoid most memory
leaks that way, but you can also say goodbye to double deletion and
pointers to rogue objects (invalid objects, already deleted objects).
Using weak_ptr, you can make sure that when an object dies, all pointers
to that object get automatically NULLed out. IMO, it's the single most
important library feature that radically improves your code quality.

I wrote a small introductory tutorial about shared_ptr:
http://tweakbits.com/articles/sharedptr/index.html

Tom
 

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

Top