Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++, copy constructor vs assignement operator

Posted on 2013-01-21
4
Medium Priority
?
378 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 240 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 1260 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

715 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