为什么这不模糊? [英] Why is this not ambiguous?

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

问题描述

有人可以告诉我为什么选择B类中的构造函数而不是A类中的运算符B

?这不是含糊不清的吗?


谢谢。

#include< iostream>


使用命名空间std;


struct A;


struct B {

B(){}

B(const A&){} //选择这个

};


struct A {

A(){ }

运算符B()const {return B();} //这不是

};


int main ()

{

A a;

B b;


b = a; //含糊不清?

}

Can some tell me why the chooses the constructor in class B over operator B
in class A? Is this not ambiguous?

Thanks.
#include <iostream>

using namespace std;

struct A;

struct B {
B() {}
B(const A&) {} // this is choosen
};

struct A {
A() {}
operator B() const {return B();} // this is not
};

int main()
{
A a;
B b;

b = a; // ambiguous?
}

推荐答案

REH写道:
可以有些人告诉我为什么选择B类中的构造函数而不是A类中的运算符B
?这不是模棱两可的吗?


我不确定我理解第一个问题。该程序格式错误

,因为b = a __ _实际上含糊不清。

谢谢。

#include< iostream>

使用命名空间std;

struct A;

struct B {
B(){}
B(const A&){} //这是选择
};

struct A {
A(){}
运算符B()const {return B();} //这不是
};

int main()
{
A a;
B b;

b = a; //含糊不清?
}
Can some tell me why the chooses the constructor in class B over operator B
in class A? Is this not ambiguous?
I am not sure I understand the first question. The program is ill-formed
because the "b = a" _is_ in fact ambiguous.

Thanks.
#include <iostream>

using namespace std;

struct A;

struct B {
B() {}
B(const A&) {} // this is choosen
};

struct A {
A() {}
operator B() const {return B();} // this is not
};

int main()
{
A a;
B b;

b = a; // ambiguous?
}



V


V




" REH" <博*** @ nowhere.net>在消息中写道

新闻:d2 ********* @ cui1.lmms.lmco.com ...

"REH" <bo***@nowhere.net> wrote in message
news:d2*********@cui1.lmms.lmco.com...
有人可以告诉我为什么选择B级施工人员在A级操作员
B
这不是含糊不清的吗?

谢谢。

#include< iostream>

使用命名空间std;

struct A;

struct B {
B(){}
B(const A&){} //这是选择
};

struct A {
A(){}
运算符B()const {return B();} //这不是
};

int main()
{
A a;
B b;

b = a; //含糊不清?
}
Can some tell me why the chooses the constructor in class B over operator
B
in class A? Is this not ambiguous?

Thanks.
#include <iostream>

using namespace std;

struct A;

struct B {
B() {}
B(const A&) {} // this is choosen
};

struct A {
A() {}
operator B() const {return B();} // this is not
};

int main()
{
A a;
B b;

b = a; // ambiguous?
}




我认为答案是在Stroustrup的The C ++ Programming Language中给出的,

p277:用户定义的转换仅在必要时才被视为

来解析呼叫。我没有标准来备份该声明,

但是如果他是正确的话,那么存在一个复制构造函数可以处理
的事实这项工作无需费心去寻找其他选择。

因此,没有遇到任何歧义。


-Howard



I think the answer is given in Stroustrup''s "The C++ Programming Language",
p277: "User-defined conversions are considered only if they are neccessary
to resolve a call." I don''t have the Standard to back up that statement,
but if he''s correct, then the fact that a copy-constructor exists that can
handle the job negates the need to bother looking at other alternatives.
Thus, no ambiguity is encountered.

-Howard


* REH:
有人可以告诉我为什么在A类中选择运算符B中的B类构造函数吗?这不是含糊不清的吗?

谢谢。

#include< iostream>

使用命名空间std;

struct A;

struct B {
B(){}
B(const A&){} //这是选择
};

struct A {
A(){}
运算符B()const {return B();} //这不是
};

int main()
{
A a;
B b;

b = a; //含糊不清?
}
Can some tell me why the chooses the constructor in class B over operator B
in class A? Is this not ambiguous?

Thanks.
#include <iostream>

using namespace std;

struct A;

struct B {
B() {}
B(const A&) {} // this is choosen
};

struct A {
A() {}
operator B() const {return B();} // this is not
};

int main()
{
A a;
B b;

b = a; // ambiguous?
}




Comeau在线编译:


Comeau C / C ++ 4。3。3(8月) 6 2003 15:13:37)ONLINE_EVALUATION_BETA1

版权所有1988-2003 Comeau Computing。保留所有权利。

模式:严格错误C ++


" ComeauTest.c",第18行:错误:来自&的多个用户定义的转换; A

to

" const B"适用:

function" A :: operator B()const"

function" B :: B(const A&)"

b = a; //含糊不清?

^


在编辑ComeauTest.c时检测到错误。


在严格模式下,使用-tused,编译失败


-

答:因为它弄乱了人们通常阅读文本的顺序。 />
问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?电子邮件?



Comeau online compilation:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 18: error: more than one user-defined conversion from "A"
to
"const B" applies:
function "A::operator B() const"
function "B::B(const A &)"
b = a; // ambiguous?
^

1 error detected in the compilation of "ComeauTest.c".

In strict mode, with -tused, Compile failed

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


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

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