C++ 使一个对象等于另一个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
making a object equal to another object
提问by TheFuzz
i know you can make two objects equal to each other when one of them is being declared. i tested this in my program. but when i went to use a assignment statement it freaked out. Can you make two objects equal to each other with a assignment statement or can you only do that when one object is being declared?
我知道当声明其中一个对象时,您可以使两个对象彼此相等。我在我的程序中对此进行了测试。但是当我去使用赋值语句时,它吓坏了。你可以用赋值语句使两个对象彼此相等,还是只能在声明一个对象时才这样做?
回答by Naveen
You have provide operator= to a class so as copy the contents of another object. For example:
您已将 operator= 提供给一个类,以便复制另一个对象的内容。例如:
class A
{
public:
//Default constructor
A();
//Copy constructor
A(const A&);
//Assignment operator
A& operator=(const A& a);
};
int main()
{
A a; //Invokes default constructor
A b(a); //Invokes copy constructor;
A c;
c = a; //Invokes assignment operator
}
回答by vpram86
Overloading assignment operator for that object can help you. (I hope you are talking about objects of same class :))
重载该对象的赋值运算符可以帮助您。(我希望你在谈论同一个类的对象:))
回答by GG.
For assignment operator just overload assignment operator according to your class implementation.
对于赋值运算符,只需根据您的类实现重载赋值运算符。
回答by Red
The object may be required to initialize or create or equated with another object when the object is dependent on other objects.
当对象依赖于其他对象时,可能需要该对象初始化或创建或等同于另一个对象。
In this case, copy constructor gives the best solution..because it does not copy the object to other object by bit by bit. Bitwise copy creates problem if the memory is allocated dynamically to the object. so, the solutions is the defining the copy_constructor in the class.The copy constuctor takes reference to an existing object of the same type as its argument, and it is used to create a new object from an existing one. Here is an examplet to equate the objects with other objects using copy constructor..
在这种情况下,复制构造函数给出了最好的解决方案。因为它不会一点一点地将对象复制到其他对象。如果内存是动态分配给对象的,则按位复制会产生问题。因此,解决方案是在类中定义 copy_constructor。复制构造函数引用与其参数相同类型的现有对象,并用于从现有对象创建新对象。这是一个使用复制构造函数将对象与其他对象等同的示例。
#include "stdafx.h"
#include "iostream"
using namespace std;
class array
{
int *ptr;
int size;
public:
array (int sz) {
ptr = new int [sz];
size =sz;
for(int index=0;index<size;index++)
ptr[index]=size;
}
~array(){
delete[] ptr;
}
array(array &a) {
int index;
ptr=new int[a.size];
for(index=0;index<a.size;index++)
ptr[index]=a.ptr[index];
cout<<"copy_constructor invoked"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
array num(10);
array x(num); //invokes copy_constructor
array y=num; //invokes copy consturctor
return 0;
}
回答by MRG
This answer applies to C#.
此答案适用于 C#。
Along with Overloading = operator you should also override equalsmethod. You should also check Guidelinesfor Overloading Equals() and Operator ==
与 Overloading = 运算符一起,您还应该覆盖equals方法。您也应该检查指导重载equals()和运营商==
public struct Complex
{
double re, im;
public override bool Equals(Object obj)
{
return obj is Complex && this == (Complex)obj;
}
public override int GetHashCode()
{
return re.GetHashCode() ^ im.GetHashCode();
}
public static bool operator ==(Complex x, Complex y)
{
return x.re == y.re && x.im == y.im;
}
public static bool operator !=(Complex x, Complex y)
{
return !(x == y);
}
}