Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++, copy constructor vs assignement operator

Posted on 2013-01-21
4
361 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
ID: 38801950
>>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
ID: 38801979
@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
ID: 38802006
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
ID: 38802857
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 131
VS2015 compilation and missing DLLs 9 161
Socket Programming (Unix) 8 140
No module found pypyodbc, 3 32
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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