基类中的运算符定义
operator definition in base class
我有以下示例代码.class bar
派生自基类foo
,并为ptr_x
分配内存,同时通过基类授予读/写访问权限。这是一个大型代码的玩具模型,其中读/写函数对于对象的所有不同变体都是相同的,但内存分配在不同的变体中略有不同。
#include <iostream>
class foo{
protected:
int *ptr_x;
public:
foo(){
std::cout << " 1) Inside foo constructor: " << ptr_x << std::endl;
}
void read()
{
std::cout << " 2) Inside read function: " << ptr_x << std::endl;
std::cout << " 3) Inside read function: " << *ptr_x << std::endl;
}
void operator=(const int rhs)
{
std::cout << " 4) Inside operator= : " << ptr_x << std::endl;
*ptr_x = rhs;
}
};
class bar: public foo{
public:
bar(int a)
: foo()
{
std::cout << " 5) Inside bar constructor: " << ptr_x << std::endl;
ptr_x = new int;
std::cout << " 6) Inside bar constructor: " << ptr_x << std::endl;
*ptr_x = a;
}
~bar()
{
std::cout << " 7) Inside bar destructor: " << ptr_x << std::endl;
if (ptr_x != NULL) {delete ptr_x; ptr_x = NULL;}
}
};
int main (){
bar a(20);
a.read();
a = 40;
a.read();
return 0;
}
当我运行代码时,我得到:
1) Inside foo constructor: 0
5) Inside bar constructor: 0
6) Inside bar constructor: 0x1d8f010
2) Inside read function: 0x1d8f010
3) Inside read function: 20
1) Inside foo constructor: 0x7f40c11e3b68
5) Inside bar constructor: 0x7f40c11e3b68
6) Inside bar constructor: 0x1d8f030
7) Inside bar destructor: 0x1d8f030
2) Inside read function: 0x1d8f030
3) Inside read function: 0
7) Inside bar destructor: 0x1d8f030
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000001d8f030 ***
我有以下问题:1(为什么代码不在基类中输入operator=
?2( 为什么对构造函数/析构函数进行第二次调用?3(为什么会有double free
问题?
我在这里做错了什么?
编辑:好的double free
问题很明显,但是为什么有两个相同内存位置的实例?这在某种程度上与operator=
有关,因为当我将其注释掉时,一切都很好。
谢谢
观察到的行为的推理:
编译器在派生类中使用转换构造函数,
bar(int a)
用于评估:
a = 40;
它接受整数40
并使用转换构造函数创建一个bar
对象,然后使用编译器为类 bar
生成的隐式复制赋值运算符 (=
( 将创建的bar
对象分配给a
。
这就是您看到对构造函数的其他调用bar
并且永远不会调用基类重载=
的原因。此外,双重自由的原因在于这里,您有多个bar
对象,这些对象一直指向为ptr_x
分配的动态内存,当其中一个对象超出范围时,将调用析构函数,该析构函数解除分配内存,但使其他对象成员指针处于悬空状态。
如何解决这些问题:
如果不希望编译器将其用于此类隐式转换,则应将转换构造函数标记为显式。
您应该遵循班级bar
的三法则。
警告:
请注意,基类中的=
运算符始终由为派生类隐式生成的=
运算符隐藏。
对于第一个问题,您应该声明 bar (int a)
作为
explicit bar(int a).
这给出了一个正确的编译错误,类 bar 应该定义=
运算符。
- 在 C++ 的自定义运算符中删除与删除[](不同于常见的删除与删除[]问题)
- 自定义运算符重载C++,无开销
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 如何使用C++将MXNET自定义运算符构建到单独的库/包中?
- 模板类的用户定义运算符上的 C++ 隐式转换
- 什么是编程语言支持定义您自己的自定义运算符?
- 如何在Qt中为矩阵类定义[ ][ ]运算符?
- 在 c++ 迭代器中,我应该同时定义运算符== 和运算符!=吗?
- 在 rxcpp 中创建自定义运算符
- 错误 C2676;在C++的二叉搜索树类中定义 ++ 运算符时遇到问题
- std::map :使用自定义运算符时更新密钥
- 最初定义运算符C++在哪里
- 如何在Tensorflow Lite中添加自定义运算符
- 如何在 rxcpp 自定义运算符中正确推断泛型
- 在模板化类之外定义运算符重载
- 为类定义之外的模板类定义运算符[]()(数组订阅)
- 是否可以在类定义之外定义运算符[]()(数组订阅)
- C++ 重新定义运算符<() 和运算符!=()
- 如何将上下文信息传递给自定义运算符<<适用于 std::iostream
- 无法在C++中定义++运算符,这里有什么问题?