English 中文(简体)
凌驾于C++的超负荷运行者之上?
原标题:override the operator overloading in C++?

Helo guys

i) 班级配电

我确实是操作员超负荷工作。

Complex c = a + b; // where a and b are object of Complex class 

基本上为经营者+(Complex&);

但我知道,例如,如何说

double c = a + 10; //where a is object of Complex class but 10 is integer / double  

我的确界定了翻一番的播种,因为我的国际民主和选举援助学会说,太多的歌剧+,它有些抱怨无法“理解”+。

它必须采用这一格式,通过<代码>double c =+ 10;。

感谢

错误信息

Error: more than one operator "+" matches these operands: 
error C2666:  Rational::operator +  : 3 overloads have similar conversions 

1> could be  const Complex Complex::operator +(const Complex &)  1> 
or  double operator +(const Complex &,double)  

the compiler can not pick based on signature ? and yes I did define it outside the class because I had one defined inside the class 感谢

问题回答
double operator+(const Complex &c, int x)
{
    //....
}

如何安置形式上的建筑者:

 Complex(float _real) : m_Real( _real ), m_Imaginary(0){}

因此,任何可归入<代码>float的价值均可作为<编码>Complex加以标示。 然后,你不需要为各类类型的超负荷运营商。 您为<代码>Complex写的稿件就足够了。

你重现了模棱两可的超载错误的原因是,你有<条码>>代号+。 可在<代码>Complex 或Complex double上添加两项备选案文,但您又试图添加<代码>Complex和int。 汇编者可决定其是否更好地将<代码>int改为Complex,以首先使用或到杜布尔,并使用第二版。

为避免这种情况,您需要为您可能希望添加到<条码>/代码>的所有类型确定超载的<条码>。 (英、浮、长、未签名......) 或者不超负荷的<代码>operator+首先——仅界定一个SINGLE <代码>operator+,增加两个<代码>Complex,让类型转换处理所有其他情况。

多载operator+

double Complex::operator+(double rhs)

如果你计划这样做,你可能会想做以下工作。 首先,我们界定如下(除了允许第一和第二个歌剧团进行默示转换的类别外),NOT界定了任何其他操作者+,如操作者+(Complex,double):

Complex operator+(const Complex& a, const Complex& b) {
  // ..
}

同时界定了含蓄构体:

Complex(double a) : real(a), imag(0) {}

然后界定转换操作员(如小麦指出的,这可被视为一种不良的方案规划做法,我同意这样做;因此,如果不需要最后转换成双倍,则忽略:

operator double() const { return real; }

这将自动支持double c = a_complex + 10; and double c = 10 + a_complex; 10,即将使用隐含构造者和算术决心+(const Poly& consttric&,结果将自动翻成倍。

P.S. You may also Defin Complex&营运人+=(const compounds& o){ /*. */} within the category and use this to implementing operator+ above.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签