Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My situation:
class myClass()
{
public: myClass(void);
        myClass getMyClass();
        int number;
};


myClass::myClass()
{
   number=0;
}

myClass myClass::getMyClass(){
//what should i do here so that i can copy the class a to class b
//when i use b=a.getMyClass();
//i try return myClass() but it return the default constructor.
}


int main(){
myClass a,b;
a.number=5;
b=a.getMyClass();
cout<<b.number;
}
Posted
Updated 27-Sep-10 21:29pm
v2
Comments
Hiren solanki 28-Sep-10 2:29am    
changed word 'instant' to 'instance' inside subject line.
Aescleal 28-Sep-10 2:53am    
First of all sort out what you want - a and b are objects, not classes. What's the purpose of "getMyClass" when it's actually returning an object?

Just use the copy constructor and assignment operator. For your class you can get away with using the compiler generated ones. However you'd have to remove your default constructor for that. If you want to see what they'd look like...:

class A
{
    public:
       A( int n ) : n_( n ) {}
       A( const A &copy_from ): n_( copy_from.n_  ) {}
       A &operator=( const A &copy_from )
       {
           A temp( copy_from );
           std::swap( n_, temp.n_ );
           return *this;
       }
       friend std::ostream &operator<<( std::ostream &str, const A &a )
       {
           return str << a.n_;
       }
    private:
        int n_;
};

int main()
{
	A a( 5 ), b( 4 );

	std::cout << a << std::endl << b << std::endl;

        // Look ma, no special syntax needed!
	a = b;

	std::cout << a << std::endl << b << std::endl;
}


Note that the assignment operator is a bit more complicated that it needs to be - I tend to write them like that these days to keep them exception safe when the class gets more complicated.

In response to Volodya's comment below...

Another benefit of the "copy and swap" style of assignment operator is that you don't need to check for self assignment. If you look at older C++ texts (pre-1999 or so when the idiom was used as part of the solution to Tom Cargill's "aaarrggghhh, exceptions break everything!" challenge) you'll see assignment operators written like this:

A &amp;operator=( const A ©_from )
{
    if( ©_from != this )
    {
        n_ = copy_from.n_;
    }
    return *this;
}


The idea here was to make sure that when you're dealing with pointer members and you don't do something daft like:

A a( 10 );

a = a;


you won't cause a memory leak or end up double referencing an object or a deleted object. (It's more likely this will happen through through an alias, not many of us are daft enough to do it directly).

Copy and swap removes that requirement. If you follow the code you'll see that all data is copied first which deals with allocating and copying new sub-objects. After the old contents of the object are swapped with the temporary the temporary's destructor cleans up any sub objects from the original object. So the copy step handles allocating new sub objects, the swap step handles freeing old sub objects.

Cheers,

Ash
 
Share this answer
 
v2
Comments
voloda2 28-Sep-10 5:11am    
Ok, if you want to be absolutely correct, you should also check reference in assignment operator against this pointer...
Aescleal 28-Sep-10 5:15am    
Nope - one of the benefits of the copy and swap idiom is that you don't have to check if( ©_from != this ) - it works perfectly well without that.

Cheers,

Ash
voloda2 28-Sep-10 5:31am    
But this is adding some overhead for big objects. You are creating a temporary copy and thereafter there is a check in std::swap STL function about the references.

Sorry, but I don't see big benefits about it...

Regards Vlada
voloda2 28-Sep-10 6:01am    
There are for some interesting points in your post. Thanks for deep explanation :-).

Regards,
V.
tuolesi 28-Sep-10 7:05am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
You must use return *this;.

But instead you should implement copy constructor and assignment operator:

myClass& myClass::operator=(const myClass& src)
{
  number = src.number;
  return *this;
}

myClass::myClass(const myClass &src)
{
  *this = src;
}
 
Share this answer
 
v4
Comments
Aescleal 28-Sep-10 2:52am    
Constructors can't return anything.
voloda2 28-Sep-10 2:53am    
You are right. Code fixed. Thanks.
tuolesi 28-Sep-10 2:57am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
tuolesi 28-Sep-10 2:57am    
thx for your helps.
Aescleal 28-Sep-10 4:27am    
Won't that cause an infinite recursion?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900