English 中文(简体)
为什么不得不在C++中超载转让操作员?
原标题:Why don t i have to overload the assignment operator in c++?

我的守则是:

class Example {
 ...declarations/definitions...
};

Example operator + (Example e)
{
 Example temp;
 ...do addition...
 return temp;
}

//main
void main()
{
 Example a(1,2);
 Example b(3,4);
 Example c;
 c = a+b;
}

Now what confuses me is that I had to overload the + operator for it to work with the vectors but how about the equal = sign?? I am assigning one vector to another, shouldn t i be overloading it too?? Or is it provided already to us? Thanks.

最佳回答

如果您的班级被指定为违约,则该语文自动为“operator=(例如,如果该校有任何参考成员,则该校在未经你特别工作的情况下便可转让)。 全部是分配基数和每个成员。

问题回答

如果您不宣布某类复印机,那么你就会被默示地宣布为您(如果你曾经使用过的话,就会暗中界定)。

如果可能的话,暗中操作者将指派每个物体的成员;否则(如果有些成员是参考,或者con<>t,或者有私人或删除的复印机操作者),则你就会出现汇编错误。

同样,如果你不申报复印件,那么你就会拿到一个暗中的东西,每个成员都会复印。 你的代码正在使用<代码>operator+,以复制论点和收益价值。

指派经营人与影印施工商和违约建筑商一起违约。 它将每个数据成员的价值分配给另一个物体的相应数据成员(即浅印本)。

Astd:vector? 如果是,<代码>std:vector在违约时执行转让操作人。 单页:

"Assigns a copy of vector x as the new content for the vector object.

该呼吁之前病媒目标所含内容被删除,如果是,则由病媒X中所含内容的复制件取而代之。

在向该成员职能发出呼吁后,矢量和矢量X将具有相同的规模,并与其他各异。

一天,我会首先提出答案。





相关问题
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?

热门标签