Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Implicit operators on a class
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Dave  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 23:18
Newsgroups: comp.lang.c++.moderated
From: Dave <thedaverud...@gmail.com>
Date: Thu, 5 Nov 2009 17:18:24 CST
Local: Thurs 5 Nov 2009 23:18
Subject: Implicit operators on a class
Hey all,

So, if I create a class, and give it a constructor, like so:

class Thing
{
public:
     Thing( int );

};

Then I can do implicit conversions with that class, like so

int main()
{
     int i = 10;
     Thing t = i;

}

However, I ran into a case where I have the following

class Skeleton
{
   public:
     Skeleton() : _root( NULL ){}
     Skeleton( const Skeleton& skeleton ) : _root( new Joint
( *skeleton._root ) ){}
     ~Skeleton(){ if( _root( NULL ) ) delete root; }

     Joint* _root;

};

int main()
{
     Skeleton skel1;
     Skeleton skel2;
     skel2 = skel1;

     std::cout << skel1._root << std::endl;
     std::cout << skel2._root << std::endl;

}

In doing this, my copy constructor isn't actually getting called. What
does happen is that skel2.root now has the same address as skel1.root,
so I get problems later on when the two objects fall out of scope and
the destructor does delete on the same address twice.

I instead have to create a separate operator= for the class. Am I
missing something, or why is the explicit assignment operator
necessary here?

{ Short answer: you need an assignment operator. The Law of The Big
  Three states "A class with any of {destructor, assignment operator,
  copy constructor} generally needs all 3"; see FAQ 27.10. -mod/sk }

Thanks.

Dave

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikola Smiljanić  
View profile   Translate to Translated (View Original)
 More options 6 Nov, 15:37
Newsgroups: comp.lang.c++.moderated
From: Nikola Smiljanić <popiz...@gmail.com>
Date: Fri, 6 Nov 2009 09:37:16 CST
Local: Fri 6 Nov 2009 15:37
Subject: Re: Implicit operators on a class

> I instead have to create a separate operator= for the class. Am I
> missing something, or why is the explicit assignment operator
> necessary here?

Skeleton skel1;
Skeleton skel2 = skel1; // this calls copy constructor

Skeleton skel1;
Skeleton skel2;
skel2 = skel1;  // this calls assignment operator

http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google