Solved

C++, copy constructor vs assignement operator

Posted on 2013-01-21
4
365 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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