我有个关于操作员超载的问题。下面是我的示例代码。如果你能读懂,我的问题在下面。
//class definition
class example
{
private:
int a ; // Defined in FeetInches.cpp
public:
void seta(int f)
{
a = f;
}
example operator + (const example &); // Overloaded +
int geta()
{
return a;
}
};
example example::operator + (const example &right)
{
example temp;
temp.a = a + right.a;
return temp;
}//主要
#include "header" //this is the class definition above
#include <iostream>
using namespace std;
int main()
{
example r;
r.seta(1);
example s;
s.seta(1);
example t;
t = r + s;
t = r + 1; //if included it won't compile
t = 1 + r; //if included it won't compile
int x = t.geta();
cout << x;
cin.get();
return 0;
}我理解,当您试图使用操作符重载将对象添加到一起时,它们应该是相同的。
下面是一个问题:我最近看到了,当对象在它编译的操作符的一边时,而在另一边时,它没有编译。
它编译的t = r + 1;。t = 1 + r;,它没有。
(我也知道,在我的示例中,这两种方法都不起作用,只是更容易用代码来构建问题。)
当对象位于操作符的一边时,操作符重载是如何编译的,而当对象位于另一边时,操作符重载是如何编译的。
谢谢
发布于 2014-03-05 21:33:07
如果t = r + 1;定义了匹配的operator+()方法,则意味着t = r.operator+(1);,否则就意味着t = operator+(r, 1);。它没有编译,因为您没有定义任何+操作符,这些操作符的左边是example,右边是int,例如:
// as a class method:
class example
{
...
public:
...
example operator + (int right) const;
};
example example::operator + (int right) const
{
example temp;
temp.seta(a + right);
return temp;
}或者:
// as a global operator:
class example
{
...
public:
...
friend example operator + (const example& left, int right);
};
example operator + (const example& left, int right)
{
example temp;
temp.seta(left.a + right);
return temp;
}如果您已经定义了一个以int作为输入的构造器,那么当您将int值传递给example::operator+(const example&)方法时,编译器可能已经创建了一个临时int,例如:
class example
{
...
public:
example (int f);
...
example operator + (const example& right);
};
example::example::(int f)
: a(f)
{
}
example example::operator + (const example& right)
{
example temp;
temp.a = a + right.a;
return temp;
}同样,t = 1 + r;的意思是t = operator+(1, r); (因为1不是类类型)。它没有编译,因为您没有定义一个全局+操作符,该操作符在左边接受一个int,在右边定义一个example:
class example
{
...
public:
...
friend example operator + (int left, const example& right);
};
example operator + (int left, const example& right)
{
example temp;
temp.a = left + right.a;
return temp;
}发布于 2014-03-05 21:27:51
如果您详细地重写了违规语句,则如下所示:
t.operator=(1.operator+(r));这没有多大意义,也让编译器感到困惑。
由于混淆,它可以将数字1转换为example的实例,也可以将变量r转换为整数。不幸的是,您在课堂上也没有提供足够的信息。
如果您为类提供了一个接受整数的构造函数,那么情况可能不会那么令人困惑:
class example
{
public:
example(int new_value) : a(new_value)
{ ; }
};现在,您已经为编译器提供了一个将整数转换为example的方法。
另一种选择是提供一个浇铸或转换操作符:
class example
{
public:
int operator int (const example& e)
{
return e.a;
}
};还有其他选择,例如创建一个接受整数的加法。
编辑1:
如果您正在设计一个单元类,请注意常量。你不知道常数在什么单位,如英尺或英寸。编译器将无法提供帮助,因为数值常量没有与它们关联的单元。
发布于 2014-03-05 21:29:52
这很简单,当您重载操作符时,您可以通过两种方式来完成:
首先就像你做的那样。您为类插入了重载运算符。这样,左参数必须是该类的对象。为什么?因为当您调用这个运算符时,您可以将其作为类中每个函数的调用来执行。您不能以其他方式进行此操作,因为您不能通过其他类型调用此函数。
第二,您让重载的操作符作为类的朋友。在你的课堂上,你写了这句话:
friend example& operator + (const example &left, const example &right);在你的课程定义之后,你把这个:
example& operator + (const example &left, const example &right){...}因此,如果要添加整数或其他类型,则必须修改运算符以如下方式添加:
example& operator + (const example &left, const int right){...}或者这个:
example& operator + (const int left, const example &right){...}如果您想从操作器的左边或硬边添加int,请选择其中一个或两者。
https://stackoverflow.com/questions/22209434
复制相似问题