Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, hope you are at good state of health . Actually I’m confused b/w Copy Constructor and Overloaded Assignment Operator ..

I’ve written something, where copy constructor worked well
But Why Not The Assignment Operator . :?


C++
class OList {
public: 
	OList(){
	
		MaxSize = 0;
		listArray = NULL;
		cout << "Def -> constructor\n";
		if (listArray == NULL) throw OUT_OF_MEMORY;
		mSize = 0;
	}
	OList(int size);  		    
	~OList () {delete [] listArray;}; 
	bool isFull() {return mSize == MaxSize;}
	bool isEmpty() {return mSize == 0; }
	int  length();
	bool remove(int value); // ??
	void insert(const int value);		 
	bool isPresent(const int value);
	const OList &operator =(const OList &rhs); //Assignment Operator.. !!
	OList(const OList &cpy);  //Copy constructor .. 

private:
	int MaxSize;	  // max List size
	int *listArray;	 // array to store the data
	int mSize;	// no. of elements in the List
	
	int search(const int value);
	// serach is to be invoked through the iterator can also be used in the remove method
	int getData(int i);
	void setData(int i, int data);
	bool removeAt (int index);
//	bool removeAt1 (int index);
};


-> Copy Constructor
C++
OList::OList(const OList &cpy)
{
	cout << "COPy CONSTRUCTOR CALLED !! " << "\n\n";

	MaxSize = cpy.MaxSize;
	mSize = cpy.mSize;
	listArray = new int[MaxSize];
		// check for NULL
		if (listArray == NULL) throw OUT_OF_MEMORY;
		mSize = 0;
		for (int i = 0; i < MaxSize; i++)
			listArray[i] = cpy.listArray[i];
}


-> Overloaded Assignment Operator
C++
const OList& OList::operator=(const OList &rhs) 
{
	if (this != &rhs)
	{
		delete [] listArray;
		MaxSize = rhs.MaxSize;
		mSize = rhs.mSize;
		listArray = new int[MaxSize];
		// check for NULL
		if (listArray == NULL) throw OUT_OF_MEMORY;
		mSize = 0;
		for (int i = 0; i < MaxSize; i++)
			listArray[i] = rhs.listArray[i];
	}
	return *this;

	cout << " Assignment Operator Called. !! " << "\n\n";
}


-> Main ()
C++
int main() {
//	int data;

	OList myList(5);
	OList weed(myList); // Copy Constructor Called .. 

	// how to invake Assignment Operator :?
	OList test = myList; // Again Copy Constructor Called .. W H Y ?!


return 0;
}


Thanks For Your Consideration Sir .. :)
Posted
Updated 20-Sep-14 12:47pm
v2
Comments
Philippe Mori 23-Sep-14 20:43pm    
By the way, in your case, for exception safety, you should uses RAII for your assignement operator.

THat is you should create a temporary object using copy constructor and then swap variables between the temporary object and current object.

The statement as a whole is an initialization, therefore the only methods being considered are constructors.

See http://www.gotw.ca/gotw/001.htm[^] for a discussion of the various ways how variables may be initialzed.

Consequently, if you deliberaty want to invoke the assignment operator, you have to separate the object construction from the assignment as suggested in solution 1.
 
Share this answer
 
I believe the compiler is doing a bit of optimisation for you, rather than calling the default constructor on test and the calling the assignment operator, it just calls the copy constructor.
If you had
OList MyList(5);
OList MyOtherList;
MyOtherList = MyList;

where the OList had already been constructed, then the assignment operator would be called.
 
Share this answer
 
Comments
Usman Hunjra 21-Sep-14 6:36am    
Thank you Sir .. :)
Stefan_Lang 23-Sep-14 7:49am    
Actually, no - it is not an optimization, it is what the C/C++ standard states: the equal sign in this case is not an assignment but one of the ways to initialize an object.

This distinction is important, because an optimization is optional, and it is important for the programmer to know when the assignment operator is called, and when the copy constructor!
Philippe Mori 23-Sep-14 20:36pm    
Even though the copy construction is optimized out, it must be accessible so if the copy constructor is private, the compiler would complain for the initialization of test variable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900