_3DTocka operator=(_3DTocka _3D){
swap(*this, _3D);
return *this;
}
//main()
_3DTocka _3Dx1(5, 9, 2), _3Dx2(_3Dx1); // first one is using constructor, second one copy constuctor and they both have 5,9,2
_3Dx1 = _3Dx2;
_3DTocka 是类的名称。代码编译,然后程序在运行时立即给出 SIGSEGV 错误。IDE 转到 move.h,第 167 行,代码:swap(_Tp& __a, _Tp& __b)
最佳答案
这是一个无限递归。函数 swap() 的工作原理如下:
void swap(Type & a, Type & b) {
Type tmp = a; \
a = b; -> here it calls your operator=
b = tmp; /
}
您必须将 _3D 中的所有类属性分配给此
this->a = _3D.a;
this->b = _3d.b;
...
或者您可以使用 memcpy(this, &_3D, sizeof(_3D))
,但前提是您的类不包含其他对象,而只包含基本类型。
关于c++ - 重载 operator= 时出现 SIGSEGV 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23362794/