为什么没有产生歧义错误 [英] why no ambiguity error generated

查看:70
本文介绍了为什么没有产生歧义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下计划:


#include< iostream>


使用命名空间std;


class my_complex

{

public:

friend ostream& operator<(&ostream& os,const my_complex& c);

my_complex(double r,double i = 10.0):re(r),im(i){}

my_complex(const my_complex& rc):re(rc.re),im(rc.im){}

my_complex operator +(const my_complex& r);

朋友my_complex operator +(const my_complex& c1,const

my_complex& c2);


private:

double re;

double im;

};


ostream& operator<(&ostream& os,const my_complex& c)

{

os<< re = << c.re<< " im =" << c.im<< endl;

返回os;

}


my_complex my_complex :: operator +(const my_complex& r)

{

cout<< 来自成员运营商+ << endl;

返回my_complex(re + r.re,im + r.im);

}


my_complex operator +( const my_complex& c1,const my_complex& c2)

{

cout<< 来自朋友运营商+ << endl;

my_complex c(c1.re + c2.re,c1.im + c2.im);

返回c;

}


int main()

{

my_complex obj(1,2);


cout<< 5.0 + obj;


cout<< obj + 5.0;


返回0;

}


VC +下的上述程序的输出+2005快递版

和g ++,来自朋友运营商的
+

re = 6 im = 12

来自会员运营商+

re = 6 im = 12


我认为表达式''5.0
$会产生歧义错误b $ b + obj''和''obj + 5.0''因为我已将operator +()定义为

成员函数和友元函数。但我的理解似乎是错误的。请澄清我哪里出错了。


谢谢

V.Subramanian

解决方案

12月3日晚上7:21,subramanian10 ... @ yahoo.com,印度

< subramanian10 ... @ yahoo.comwrote:


考虑以下程序:


#include< iostream>


using namespace std;


class my_complex

{

public:

friend ostream& operator<(&ostream& os,const my_complex& c);

my_complex(double r,double i = 10.0):re(r),im(i){}

my_complex(const my_complex& rc):re(rc.re),im(rc.im){}

my_complex operator +(const my_complex& r);

朋友my_complex operator +(const my_complex& c1,const

my_complex& c2);


private:

double re;

double im;


};


ostream& operator<(&ostream& os,const my_complex& c)

{

os<< re = << c.re<< " im =" << c.im<< endl;

返回os;


}


my_complex my_complex :: operator +(const my_complex& r)

{

cout<< 来自成员运营商+ << endl;

返回my_complex(re + r.re,im + r.im);


}


my_complex operator +(const my_complex& c1,const my_complex& c2)

{

cout<< 来自朋友运营商+ << endl;

my_complex c(c1.re + c2.re,c1.im + c2.im);

返回c;


}


int main()

{

my_complex obj(1,2);


cout<< 5.0 + obj;


cout<< obj + 5.0;


返回0;


}


上述程序的输出在VC ++ 2005 Express Edition

和g ++下,来自朋友运营商的
+

re = 6 im = 12 <来自会员运营商的
+

re = 6 im = 12

我认为表达式5.0会产生歧义错误br />
+ obj''和''obj + 5.0''因为我已将operator +()定义为

成员函数和友元函数。但我的理解似乎是错误的。请澄清我出错的地方。



由于const-correctness,这不是歧义。该成员不是一个const成员函数
。因此,第一个隐含的''this''

参数是非const的。而对于friend函数,两个参数

都是const。所以,这是你的不同之处。使成员const和你

会发现歧义。


* 12月3日晚上7:37,Abhishek Padmanabh< abhishek。 padman ... @ gmail.com>

写道:


12月3日晚上7:21,subramanian10 ... @ yahoo.com,印度"



现在我已经将对象和运算符+()成员

函数作为const。即考虑修改后的程序:


#include< iostream>


使用命名空间std;


class my_complex

{

public:

friend ostream& operator<(&ostream& os,const my_complex& c);

my_complex(double r,double i = 10.0):re(r),im(i){}

my_complex(const my_complex& rc):re(rc.re),im(rc.im){}

my_complex operator +(const my_complex& r)const;

朋友my_complex operator +(const my_complex& c1,const my_complex

& c2);


private:

double re;

double im;

};


ostream& operator<(&ostream& os,const my_complex& c)

{

os<< re = << c.re<< " im =" << c.im<<结束;

返回操作系统;

}


my_complex my_complex :: operator +(const my_complex& r)const

{

cout<< 来自成员运营商+ << endl;

返回my_complex(re + r.re,im + r.im);

}


my_complex operator +( const my_complex& c1,const my_complex& c2)

{

cout<< 来自朋友运营商+ << endl;

my_complex c(c1.re + c2.re,c1.im + c2.im);

返回c;

}


int main()

{

const my_complex obj(1,2);


cout<< 5.0 + obj;


// cout<< obj + 5.0;


返回0;

}


虽然现在我的评论模糊不清声明,

对于
''5.0 + obj'',仍然没有产生AMBIGUITY错误。我刚刚在C ++常见问题解答一书的第264页中找到了第2版​​

由Marshall Cline编写,C ++从不在

中提升成员函数调用中的this对象。如果这是原因(没有歧义),

然后请解释为什么这个规则是这样的 - 即为什么一个成员函数

在推广''这之后没有被调用''对象?


如果还有其他原因,请解释一下。


谢谢

V.Subramanian


12月4日上午10:09,subramanian10 ... @ yahoo.com,India

< subramanian10 .. 。@ yahoo.comwrote:


* 12月3日晚上7:37,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>


< snipped code>


虽然现在我的评论声明出现歧义错误,

仍然没有为
''5.0 + obj''生成了AMBIGUITY错误。我刚刚在C ++常见问题解答一书的第264页中找到了第2版​​

由Marshall Cline编写,C ++从不在

中提升成员函数调用中的this对象。如果这是原因(没有歧义),

然后请解释为什么这个规则是这样的 - 即为什么一个成员函数

在推广''这之后没有被调用''对象?


如果还有其他原因,请妥善解释。



我对此不确定。这可能是因为我无法找到

标准中的任何具体内容(不要怪它,

怪我。我根本找不到它,它必须在某处)。

但是,我没有看到''this''对象促销在哪里需要


consider the following program:

#include <iostream>

using namespace std;

class my_complex
{
public:
friend ostream & operator<<(ostream &os, const my_complex &c);
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(const my_complex &rc) : re(rc.re), im(rc.im) { }
my_complex operator+(const my_complex &r);
friend my_complex operator+(const my_complex &c1, const
my_complex &c2);

private:
double re;
double im;
};

ostream & operator<<(ostream &os, const my_complex &c)
{
os << "re = " << c.re << " im = " << c.im << endl;
return os;
}

my_complex my_complex::operator+(const my_complex &r)
{
cout << "from member operator+" << endl;
return my_complex(re + r.re, im + r.im);
}

my_complex operator+(const my_complex &c1, const my_complex &c2)
{
cout << "from friend operator+" << endl;
my_complex c(c1.re + c2.re, c1.im + c2.im);
return c;
}

int main()
{
my_complex obj(1, 2);

cout << 5.0 + obj;

cout << obj + 5.0;

return 0;
}

The output of the above program under both VC++2005 Express Edition
and g++, is

from friend operator+
re = 6 im = 12
from member operator+
re = 6 im = 12

I thought ambiguity error would be generated for the expressions ''5.0
+ obj'' and ''obj + 5.0'' because I have defined operator+() both as
member function and as friend function. But my understanding seems to
be wrong. Kindly clarify where I am going wrong.

Thanks
V.Subramanian

解决方案

On Dec 3, 7:21 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

consider the following program:

#include <iostream>

using namespace std;

class my_complex
{
public:
friend ostream & operator<<(ostream &os, const my_complex &c);
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(const my_complex &rc) : re(rc.re), im(rc.im) { }
my_complex operator+(const my_complex &r);
friend my_complex operator+(const my_complex &c1, const
my_complex &c2);

private:
double re;
double im;

};

ostream & operator<<(ostream &os, const my_complex &c)
{
os << "re = " << c.re << " im = " << c.im << endl;
return os;

}

my_complex my_complex::operator+(const my_complex &r)
{
cout << "from member operator+" << endl;
return my_complex(re + r.re, im + r.im);

}

my_complex operator+(const my_complex &c1, const my_complex &c2)
{
cout << "from friend operator+" << endl;
my_complex c(c1.re + c2.re, c1.im + c2.im);
return c;

}

int main()
{
my_complex obj(1, 2);

cout << 5.0 + obj;

cout << obj + 5.0;

return 0;

}

The output of the above program under both VC++2005 Express Edition
and g++, is

from friend operator+
re = 6 im = 12
from member operator+
re = 6 im = 12

I thought ambiguity error would be generated for the expressions ''5.0
+ obj'' and ''obj + 5.0'' because I have defined operator+() both as
member function and as friend function. But my understanding seems to
be wrong. Kindly clarify where I am going wrong.

It is not an ambiguity because of const-correctness. The member is not
a const member function. Because of that the first implicit ''this''
argument is non-const. While for the friend function, both arguments
are const. So, there''s your difference. Make the member const and you
will find the ambiguity.


* On Dec 3, 7:37 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Dec 3, 7:21 pm, "subramanian10...@yahoo.com, India"

Now I have made both the object as well as the operator+() member
function as const. ie consider the modified program:

#include <iostream>

using namespace std;

class my_complex
{
public:
friend ostream & operator<<(ostream &os, const my_complex &c);
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(const my_complex &rc) : re(rc.re), im(rc.im) { }
my_complex operator+(const my_complex &r) const;
friend my_complex operator+(const my_complex &c1, const my_complex
&c2);

private:
double re;
double im;
};

ostream & operator<<(ostream &os, const my_complex &c)
{
os << "re = " << c.re << " im = " << c.im << endl;
return os;
}

my_complex my_complex::operator+(const my_complex &r) const
{
cout << "from member operator+" << endl;
return my_complex(re + r.re, im + r.im);
}

my_complex operator+(const my_complex &c1, const my_complex &c2)
{
cout << "from friend operator+" << endl;
my_complex c(c1.re + c2.re, c1.im + c2.im);
return c;
}

int main()
{
const my_complex obj(1, 2);

cout << 5.0 + obj;

// cout << obj + 5.0;

return 0;
}

While I am getting ambiguity error now for the commented statement,
there is still NO AMBIGUITY error generated for
''5.0 + obj''. I just now found in page 264 in the book "C++ FAQs" 2nd
Edition by Marshall Cline that C++ never promotes the ''this'' object in
a member function invocation. If this is the reason(for no ambiguity),
then please explain why this rule is so - ie why a member function is
not called after promoting the ''this'' object ?

If there is any other reason, kindly explain.

Thanks
V.Subramanian


On Dec 4, 10:09 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

* On Dec 3, 7:37 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>

<snipped code>

While I am getting ambiguity error now for the commented statement,
there is still NO AMBIGUITY error generated for
''5.0 + obj''. I just now found in page 264 in the book "C++ FAQs" 2nd
Edition by Marshall Cline that C++ never promotes the ''this'' object in
a member function invocation. If this is the reason(for no ambiguity),
then please explain why this rule is so - ie why a member function is
not called after promoting the ''this'' object ?

If there is any other reason, kindly explain.

I am not sure on this. That could be the reason as I could not find
anything specific regarding this in the standards (don''t blame it,
blame me. I just could not find it, it must be there somewhere).
However, I am failing to see where the ''this'' object promotion is
required.


这篇关于为什么没有产生歧义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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