关于表达式转换的问题 [英] Question about conversion in expressions

查看:49
本文介绍了关于表达式转换的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


只是另一个问题。假设我有2个班级(不完整的代码):


A级{

A(const B& b);

A& ; operator =(const A& a);

};


B级{

B(const A& a);

朋友B操作员+(const B& b1,const B& b2);

};


如果我有代码喜欢:


{

A x,y,z;


x = y + z;

}


A没有+运算符.B的ctor可能使'/ y'的临时对象成为

'和''z'',然后调用B的+运算符

添加x和y,结果转换回A类,其ctor

然后分配到x?或者我在这里说完全废话了吗?


任务指示牌,


Jeroen

解决方案

2月28日下午4:50,Jeroen< no_m ... @ thanx.comwrote:


大家好,


只是另一个问题。假设我有2个班级(不完整的代码):


A级{

A(const B& b);

A& ; operator =(const A& a);


};


B级{

B(const A& amp; ; a);

朋友B操作员+(const B& b1,const B& b2);


};


如果我有以下代码:


{

A x,y,z;


x = y + z;


}


A没有+运算符.B的ctor可能是

''y'和''''的临时对象,然后调用B的+运算符

添加x和y,结果转换回类A用它的ctor

然后分配给x?或者我在这里说完全废话了吗?


任务指示,这是
Jeroen



编译器不能推断它应该如何添加z和y;也许

类似于B的其他类。我建议使用以下两种方法之一

行:


x = B(y)+ z;

x = y + B(z);


与你所做的编译器相比,这些情况是

能够选择''B运算符+(const B&,const B&)''。


终结者schreef:


2月28日下午4:50,Jeroen< no_m ... @ thanx.comwrote:


>大家好,

只是另一个问题。假设我有2个类(不完整的代码):

A类{
A(const B& b);
A& operator =(const A& a);

};

B类{
B(const A& a);
朋友B运营商+ (const B& b1,const B& b2);

};

如果我的代码如下:

{
A x,y,z;

x = y + z;

}
对于A.没有+运算符。是否有可能B的ctor使'/ y'和''''成为临时对象,然后调用B的+运算符来添加x和y,结果转换回A类用它的ctor
然后分配到x?或者我在这里说完全废话了吗?

对于任何指针来说,这都是好的。

Jeroen



编译器不能推断它应该如何添加z和y;也许

其他类似于B的类。我建议以下任何一个

行:


x = B(y)+ z;

x = y + B(z);


在这两种情况下都与你相反编译器是否已经能够选择''B运算符+(const B&,const B&)''。



好​​的,所以至少有一个变量应与可用的算术运算符相匹配,以确保它有效。谢谢!




" Jeroen" < no ***** @ thanx.com在消息新闻中写道:45e58df7 @ cs1 ...


终结者schreef:


> 2月28日下午4:50,Jeroen< no_m ... @ thanx.comwrote:


>>大家好,

只是另一个问题。假设我有2个类(不完整的代码):

A类{
A(const B& b);
A& operator =(const A& a);

};

B类{
B(const A& a);
朋友B运营商+ (const B& b1,const B& b2);

};

如果我的代码如下:

{
A x,y,z;

x = y + z;

}
对于A.没有+运算符。是否有可能B的ctor使'/ y'和''''成为临时对象,然后调用B的+运算符来添加x和y,结果转换回A类用它的ctor
然后分配到x?或者我在这里说完全废话了吗?

对于任何指针,任何指针,

Jeroen


编译器无法推断它应该如何添加z和y;也许还有类似于B的其他类。我建议使用以下任一行:

x = B(y)+ z;
x = y + B(z);

在任何一种情况下,与你所做的相反,编译器能够选择''B运算符+(const B&,const B& ;)''。



好​​的,所以至少有一个变量应与可用的算术运算符相同,以确保它有效。谢谢!



但你应该问自己的问题是我真的应该这样做吗?你正走向隐式转换,这是一个危险的b $ b b事物,也是一个相当可疑的设计。如果A和B不相关,这是你的设计所表明的,那你为什么要将

转换为A并且可能会返回。为什么你需要编译器默默地执行此操作,这更令人怀疑。如果A& B不相关,那么它们也不应该共享实现,例如+ op。我会

强烈建议你重新考虑你的设计,因为恕我直言这看起来很像一个严重的设计缺陷的开始,这可能提供

你无休止的调试时间可以说是有争议的。


干杯

克里斯


Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);
};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
};

If I have code like:

{
A x, y, z;

x = y + z;
}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of ''y'' and ''z'', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen

解决方案

On Feb 28, 4:50 pm, Jeroen <no_m...@thanx.comwrote:

Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of ''y'' and ''z'', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen

The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick ''B operator+(const B&,const B&)''.


terminator schreef:

On Feb 28, 4:50 pm, Jeroen <no_m...@thanx.comwrote:

>Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of ''y'' and ''z'', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen


The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick ''B operator+(const B&,const B&)''.

OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!



"Jeroen" <no*****@thanx.comwrote in message news:45e58df7@cs1...

terminator schreef:

>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx.comwrote:

>>Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of ''y'' and ''z'', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen


The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick ''B operator+(const B&,const B&)''.


OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!

But the question that you should rather ask yourself is should I really do
this? You''re walking towards implicit conversion, which is a dangerous
thing, and also towards a rather questionable design. If A and B are not
related, which is what is indicated by your design, then why would you want
to convert A to B and probably back. It''s even more questionable why you
want the compiler to do this silently. If A & B are not related then they
also should not share implementations, like the + op for example. I''d
strongly suggest that you reconsider your design because IMHO this looks
very much like the beginning of a serious design flaw, which could provide
you the arguable pleasure of endless debugging hours.

Cheers
Chris


这篇关于关于表达式转换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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