现在的位置: 首页 > 综合 > 正文

C++中赋值运算符默认的返回值类型

2013年08月30日 ⁄ 综合 ⁄ 共 4112字 ⁄ 字号 评论关闭

在C++中赋值运算符默认的返回值类型是typename&,如
int i;
i=3;
实际上i=3返回的是指向i的引用,这样可以把i=3用于持续操作,如
if(i=strlen(str)){
   ... i ...
}
上面代码中的if条件语句中既进行了判断又给i进行了赋值,而且可以用于串联式的赋值
x=y=z=3;
而实际上这里种操作并不要求"="返回const typename&,只要返回int值就可以了。为什么非要返回一个引用呢?请考虑一个具有复杂数据结构的类Object,如果进行这样的操作:

Object a,b;
Object c=(b=a);
如果=返回的不是引用类型,则会生成一个临时对象,请看下面的代码:

class ObjectA
{
private:
	std::string _name;
public:

	ObjectA(const ObjectA& a) : _name(a._name)
	{
		cout<<_name.c_str()<<" : ObjectA::ObjectA(ObjectA&)"<<ENDL; pre };< } b="a;" a(?a?),b(?b?); ObjectA { Test(void) void static ObjectA::~ObjectA()?<<endl; : cout<<_name.c_str()<<? ~ObjectA() *this; return _name="a._name;" ObjectA::operator='()"<<endl;' a) ObjectA& operator="(const" ObjectA::ObjectA(string)?<

Test输出结果为:

a : ObjectA::ObjectA(string)
b : ObjectA::ObjectA(string)
b : ObjectA::operator=()
a : ObjectA::ObjectA(ObjectA&)
a : ObjectA::~ObjectA()
a : ObjectA::~ObjectA()
a : ObjectA::~ObjectA()

由上的输出结果可以看出,在计用operator=然后使用返回值给b赋值的时候,通过拷贝构造函数生成了一个临时对象。所以将operator=的返回值设为引用类型是非常必要的。

但设为引用类型又会产生如下问题。如下面的语句是合法的:
Object a,b,c,d;
((a=b)=c)=d;
那程序的执行结果是什么呢,就是只影响了a,实际是将d复制给了a,b和c没有任何改变。我们可以可以阻止这种行为呢?可以的,只要把operator=的返回值设为const Object&就可以了。这样,再使用类似的语句时编译器会给出错误信息:

Error    2    error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Object'  (or there is no acceptable conversion)   

程序代码如下:

class Object
{
private:
	int value;
public:
	Object(int i):value(i)
	{
	}

	const Object& operator=(const Object& obj)
	{
		value = obj.value;
		return *this;
	}

	const Object& operator=(const int i)
	{
		value = i;
		return *this;
	}

	int getValue(void) const
	{
		return value;
	}

	static void Test(void)
	{
		Object a(1),b(2),c(3);
		Widget x(1), y(2), z(3);

		a  = b = c = 4;
		//((a=b)=c)=4;		// Error	2	error C2678: binary '=' :/
							no operator found which takes a left-hand operand of type 'const Object' /
							(or there is no acceptable conversion)	
		
		
	}

但这样只是给客户代码一些限制而已,但是不是绝对安全呢?不是的,客户代码仍然可以使用下面的语句进行((a=b)=c)=d;这样操作:
const_cast(const_cast(a=b)=c)=4;
但毕竟比仅使用非const引用要更安全多了。

综上所述,本人认为在Scott Meyers的《Effective C++》的第10条“Have assignment operators return a reference to *this",最好改为"Have assignment operators return a const reference to *this"。不知道是不是忽略了其它因素,查查再说吧。Scott Meyers这样说是想让自定义的数据类型(类)符合C++的默认行为,但C++的默认行为也不一定是最好的嘛。因为,使((a=b)=c)=4;这样的语句合法,好像没有什么意思,而且得不到任何好处,为何不限制掉呢?

在C++中赋值运算符默认的返回值类型是typename&,如
int i;
i=3;
实际上i=3返回的是指向i的引用,这样可以把i=3用于持续操作,如
if(i=strlen(str)){
   ... i ...
}
上面代码中的if条件语句中既进行了判断又给i进行了赋值,而且可以用于串联式的赋值
x=y=z=3;
而实际上这里种操作并不要求"="返回const typename&,只要返回int值就可以了。为什么非要返回一个引用呢?请考虑一个具有复杂数据结构的类Object,如果进行这样的操作:

Object a,b;
Object c=(b=a);
如果=返回的不是引用类型,则会生成一个临时对象,请看下面的代码:

class ObjectA
{
private:
	std::string _name;
public:

	ObjectA(const ObjectA& a) : _name(a._name)
	{
		cout<<_name.c_str()<<" : ObjectA::ObjectA(ObjectA&)"<<ENDL; pre };< } b="a;" a(?a?),b(?b?); ObjectA { Test(void) void static ObjectA::~ObjectA()?<<endl; : cout<<_name.c_str()<<? ~ObjectA() *this; return _name="a._name;" ObjectA::operator='()"<<endl;' a) ObjectA& operator="(const" ObjectA::ObjectA(string)?<

Test输出结果为:

a : ObjectA::ObjectA(string)
b : ObjectA::ObjectA(string)
b : ObjectA::operator=()
a : ObjectA::ObjectA(ObjectA&)
a : ObjectA::~ObjectA()
a : ObjectA::~ObjectA()
a : ObjectA::~ObjectA()

由上的输出结果可以看出,在计用operator=然后使用返回值给b赋值的时候,通过拷贝构造函数生成了一个临时对象。所以将operator=的返回值设为引用类型是非常必要的。

但设为引用类型又会产生如下问题。如下面的语句是合法的:
Object a,b,c,d;
((a=b)=c)=d;
那程序的执行结果是什么呢,就是只影响了a,实际是将d复制给了a,b和c没有任何改变。我们可以可以阻止这种行为呢?可以的,只要把operator=的返回值设为const Object&就可以了。这样,再使用类似的语句时编译器会给出错误信息:

Error    2    error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Object'  (or there is no acceptable conversion)   

程序代码如下:

class Object
{
private:
	int value;
public:
	Object(int i):value(i)
	{
	}

	const Object& operator=(const Object& obj)
	{
		value = obj.value;
		return *this;
	}

	const Object& operator=(const int i)
	{
		value = i;
		return *this;
	}

	int getValue(void) const
	{
		return value;
	}

	static void Test(void)
	{
		Object a(1),b(2),c(3);
		Widget x(1), y(2), z(3);

		a  = b = c = 4;
		//((a=b)=c)=4;		// Error	2	error C2678: binary '=' :/
							no operator found which takes a left-hand operand of type 'const Object' /
							(or there is no acceptable conversion)	
		
		
	}

但这样只是给客户代码一些限制而已,但是不是绝对安全呢?不是的,客户代码仍然可以使用下面的语句进行((a=b)=c)=d;这样操作:
const_cast(const_cast(a=b)=c)=4;
但毕竟比仅使用非const引用要更安全多了。

综上所述,本人认为在Scott Meyers的《Effective C++》的第10条“Have assignment operators return a reference to *this",最好改为"Have assignment operators return a const reference to *this"。不知道是不是忽略了其它因素,查查再说吧。Scott Meyers这样说是想让自定义的数据类型(类)符合C++的默认行为,但C++的默认行为也不一定是最好的嘛。因为,使((a=b)=c)=4;这样的语句合法,好像没有什么意思,而且得不到任何好处,为何不限制掉呢?

抱歉!评论已关闭.