operator = function [英] operator= function

查看:69
本文介绍了operator = function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我刚刚超载operator =上课,我有一个问题

一个案例......


A级

{

A& operator =(const A& obj)

{

return * this;

//请忽略此函数的逻辑

}

};


int main()

{

A a ,b

a = b; //工作正常

如果(a = b)//导致编译时错误,因为operator =()返回一个对A而不是BOOL的

引用。 />
{

printf(两个对象都是相同的\ n);

}

else

{

printf(两个对象都不同\ n);

}

}

返回(0);

}


现在,我试图重载operator =()函数,但重载只是没有意义。

所以无论如何都要解决这个问题,所以

类的用户可以像任何内置类型一样在两种情况下工作吗?


提前致谢!!!

Hi Everyone,

I was just overloading operator = for a class and i have a problem in
one case...

class A
{
A& operator=(const A& obj)
{
return *this;
// please ignore the logic of this function
}
};

int main()
{
A a,b
a = b; //works fine
if(a=b) // causes compile time error as the operator=() returns a
reference to A and not a BOOL.
{
printf("both the objects are the same\n");
}
else
{
printf("both the objects are different\n");
}
}
return(0);
}

Now, i tried to overload operator=() function, but overloading just
based on the return type doesn''t make sense.
So is there anyway to solve this problem, so that the user of the
class can get to work in both the cases just like any built in type?

Thanks in advance!!!

推荐答案

Rahul写道:
Rahul wrote:

大家好,


我只是在为一个班级重载运算符=我有一个问题在

一个案例......


A类

{

A& operator =(const A& obj)

{

return * this;

//请忽略此函数的逻辑

}

};


int main()

{

A a ,b

a = b; //工作正常

如果(a = b)//导致编译时错误,因为operator =()返回一个对A而不是BOOL的

引用。 />
{

printf(两个对象都是相同的\ n);

}

else

{

printf(两个对象都不同\ n);

}

}

返回(0);

}


现在,我试图重载operator =()函数,但重载只是没有意义。

所以无论如何都要解决这个问题,所以

类的用户可以像任何内置类型一样在两种情况下工作吗?
Hi Everyone,

I was just overloading operator = for a class and i have a problem in
one case...

class A
{
A& operator=(const A& obj)
{
return *this;
// please ignore the logic of this function
}
};

int main()
{
A a,b
a = b; //works fine
if(a=b) // causes compile time error as the operator=() returns a
reference to A and not a BOOL.
{
printf("both the objects are the same\n");
}
else
{
printf("both the objects are different\n");
}
}
return(0);
}

Now, i tried to overload operator=() function, but overloading just
based on the return type doesn''t make sense.
So is there anyway to solve this problem, so that the user of the
class can get to work in both the cases just like any built in type?



一个简单的方法是* * *提供一个转换函数,在你的''A'中键入

''bool'' 'class:


A级{

...

operator bool()const {return true; }

};


或任何其他转换函数,它产生一个可以在逻辑表达式中使用

的类型。


现在,让我问你,为什么你认为你需要在''if''

表达式中进行作业


如果(a = b)


而不是比较


if(a == b)

?或者您不知道那些是两个不同的运营商吗?


V

-

请删除资金''A'当用电子邮件回复时

我没有回复最热门的回复,请不要问

A simple way would be to *also* provide a conversion function to type
''bool'' in your ''A'' class:

class A {
...
operator bool() const { return true; }
};

Or any other conversion function that yields a type that can be used
in a logical expression.

Now, let me ask you, why do you think you need assignment in the ''if''
expression

if (a = b)

instead of comparison

if (a == b)

? Or did you not know that those are two different operators?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月4日,8:46 pm,Victor Bazarov, < v.Abaza ... @ comAcast.netwrote:
On Dec 4, 8:46 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Rahul写道:
Rahul wrote:

大家好,
Hi Everyone,


我刚刚为一个班级重载operator =我有一个问题

一个案例...
I was just overloading operator = for a class and i have a problem in
one case...


class A

{

A& operator =(const A& obj)

{

return * this;

//请忽略此函数的逻辑

}

};
class A
{
A& operator=(const A& obj)
{
return *this;
// please ignore the logic of this function
}
};


int main()

{

A a,b

a = b; //工作正常

如果(a = b)//导致编译时错误,因为operator =()返回一个对A而不是BOOL的

引用。 />
{

printf(两个对象都是相同的\ n);

}

else

{

printf(两个对象都不同\ n);

}

}

返回(0);

}
int main()
{
A a,b
a = b; //works fine
if(a=b) // causes compile time error as the operator=() returns a
reference to A and not a BOOL.
{
printf("both the objects are the same\n");
}
else
{
printf("both the objects are different\n");
}
}
return(0);
}


现在,我试图重载operator =( )函数,但重载只是

基于返回类型没有意义。

所以无论如何都要解决这个问题,以便<的用户br />
类可以像任何内置类型一样在两种情况下都能工作吗?
Now, i tried to overload operator=() function, but overloading just
based on the return type doesn''t make sense.
So is there anyway to solve this problem, so that the user of the
class can get to work in both the cases just like any built in type?



一个简单的方法就是* * *提供一个转换函数来在你的''A'中键入

''bool'' 'class:


A级{

...

operator bool()const {return true; }

};


或任何其他转换函数,它产生一个可以在逻辑表达式中使用

的类型。


现在,让我问你,为什么你认为你需要在''if''

表达式中进行作业


如果(a = b)


而不是比较


if(a == b)

?或者您不知道那些是两个不同的运营商吗?


A simple way would be to *also* provide a conversion function to type
''bool'' in your ''A'' class:

class A {
...
operator bool() const { return true; }
};

Or any other conversion function that yields a type that can be used
in a logical expression.

Now, let me ask you, why do you think you need assignment in the ''if''
expression

if (a = b)

instead of comparison

if (a == b)

? Or did you not know that those are two different operators?



是的我是。我的观点是将c ++为

内置类型提供的支持扩展到自定义类型。

int a,b;

a = b ; //工作正常

如果(a = b)//也可以正常工作,具体取决于a的值。

Yes i do. My point is to extend the support provided by c++ for
built-in types to custom types.
int a,b;
a=b; //works fine
if(a=b) // too works fine depending on the value of a.


V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要请问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Tue,2007年12月4日07:41:52 -0800,Rahul写道:
On Tue, 04 Dec 2007 07:41:52 -0800, Rahul wrote:

大家好,


我刚刚超载operator =上课,我有一个问题

一个案例...


A级

{

A& operator =(const A& obj)

{

return * this;

//请忽略此函数的逻辑

}

};


int main()

{

A a ,b

a = b; //工作正常

如果(a = b)//导致编译时错误,因为operator =()返回一个对A而不是BOOL的

引用。 />
{

printf(两个对象都是相同的\ n);

}

else

{

printf(两个对象都不同\ n);

}

}

返回(0);

}


现在,我试图重载operator =()函数,但重载只是没有意义。那么无论如何都要解决这个问题,这样这个类的用户可以像任何内置类型一样在

的情况下工作吗?
Hi Everyone,

I was just overloading operator = for a class and i have a problem in
one case...

class A
{
A& operator=(const A& obj)
{
return *this;
// please ignore the logic of this function
}
};

int main()
{
A a,b
a = b; //works fine
if(a=b) // causes compile time error as the operator=() returns a
reference to A and not a BOOL.
{
printf("both the objects are the same\n");
}
else
{
printf("both the objects are different\n");
}
}
return(0);
}

Now, i tried to overload operator=() function, but overloading just
based on the return type doesn''t make sense. So is there anyway to solve
this problem, so that the user of the class can get to work in both the
cases just like any built in type?



首先 - 对于整数类型语句

if(a = b)

测试b是否为零,不是如果a和b是相同的(在执行这个之后

它们肯定是相同的)。

第二 - 在''if''表达式中赋值的可能性更确切地说是错误的,我没有看到任何延伸这种可能性的意义。


-

Tadeusz B .Kopec(tk****@NOSPAMPLEASElife.pl)

有多少即将来临的男人有一个人知道!他们到底在哪里去了?

- 亚瑟王皮内罗爵士

First of all - for integer types statement
if (a=b)
tests if b is zero, not if a and b are the same (after executing this
they are for sure the same).
Second - possibility of assignment in ''if'' expression is rather a
misfeature and I don''t see any point in extending this possibility.

--
Tadeusz B. Kopec (tk****@NOSPAMPLEASElife.pl)
How many "coming men" has one known! Where on earth do they all go to?
-- Sir Arthur Wing Pinero


这篇关于operator = function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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