为什么不调用赋值运算符。 ?! [英] Why Assignment Operator NOT called . ?!

查看:77
本文介绍了为什么不调用赋值运算符。 ?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,希望你身体健康。实际上我很困惑b / w复制构造函数和重载赋值运算符..



我写了一些东西,复制构造函数运行良好

但为什么不是作业操作员。 :?




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 . :?


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

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

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()



-> Main ()

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 ?!





返回0;

}





谢谢您的考虑先生.. :)



return 0;
}


Thanks For Your Consideration Sir .. :)

推荐答案

整个语句是一个初始化,因此唯一考虑的方法是构造函数。



http://www.gotw.ca/gotw /001.htm [ ^ ]讨论变量可以如何初始化的各种方法。



因此,如果你想要调用赋值运算符,你必须将对象结构与赋值分开为在解决方案1中提出。
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.


我相信编译器正在为您做一些优化,而不是在 test 上调用默认构造函数并且调用赋值运算符,它只是调用复制构造函数。

如果你有

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;



其中已经构造了OList,然后调用赋值运算符。


where the OList had already been constructed, then the assignment operator would be called.


这篇关于为什么不调用赋值运算符。 ?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆