Solved

C++, copy constructor vs assignement operator

Posted on 2013-01-21
4
352 Views
Last Modified: 2013-01-22
#include <iostream>

using namespace std;

class A {
	private:
		int x;
		int y;

	public:
		A (A& argument);
		A operator=(A& arg);
		A(); 

		void print();

};

A A::operator=(A& arg){
	this ->x = arg.y;
	this ->y = arg.x;

	return *this;

}

A::A(A& argument){
	cout<<"copy constr."<<endl;
	this -> x = argument.x * argument.x;

}

A::A(){
	this->x = 30;
	this->y = 40;

}

void A::print(){
	cout<<this->x<<endl;

}


int main(){
	A a;
	A b;

	a=b; 

	a.print();
	b.print();

	A c = a; //it will trigger copy constructor. Why not a default constructor and then //assignment operator?

	c.print();



	return 0;
}

Open in new window


Thank you

panJames
0
Comment
Question by:panJames
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 80 total points
Comment Utility
>>A c = a; //it will trigger copy constructor. Why not a default constructor and then
>>//assignment operator?

Because that's the more efficient way. First calling a default c-tor and then an operator are two calls, why not doing the very same in one call? If you however want the behaviour you described, rewrite that as

A a;
//...
A c;
//...
c = a;

Open in new window


but be sure to turn off optimizations for that purpose  ;o)
0
 

Author Comment

by:panJames
Comment Utility
@jkr: I do not understand what you are saying.

(Default constructor and then assignment operator) are not the same as (a copy constructor).

They are different things... imho

panJames
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, they are - but if you write

A c = a;

Open in new window


it is more efficiant for the compiler to create a single call to the copy ctor insteaf of first calling the default ctor and then the assignment operator.
0
 
LVL 22

Accepted Solution

by:
ambience earned 420 total points
Comment Utility
Syntactically,

A c = a;

is one of the various forms that c++ allows for ** initialization ** - there's no question of optimization here - its part of the language. It may be deceiving in that it gives an impression that c is created and then a is assigned to it, but its actually another way to invoke a copy constructor like

Consider for example:

class A
{
public:
      A() {};
      void operator=(const A& a) {}

private:
      A(const A& a) {}
};

void main()
{
      A a;
      A b = a; // This will fail even though there is a public assignment operator available to the compiler
}

For most practical purposes

A b = a;

is the same as

A b(a);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now