Solved

C++, copy constructor vs assignement operator

Posted on 2013-01-21
4
373 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
[X]
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
  • 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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