Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

Pointers and reference question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 20 Apr 2006 16:56 GMT
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.
Tamas Demjen - 20 Apr 2006 18:14 GMT
> 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
Abubakar - 24 Apr 2006 10:45 GMT
Thanks for the code.

Ab.

> > I have checked the code and both versions seem to work fine.
>
[quoted text clipped - 41 lines]
>
> Tom

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.