我有以下示例代码。类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=
有关,因为当我评论它一切都很好。
由于
谢谢。这是否意味着我总是必须为派生类定义'='运算符?什么是运营商是这样的? – GradGuy 2012-02-17 06:44:39
@GradGuy:如果你不为你的派生类重新定义'='运算符,编译器会隐式生成它,从而隐藏基类'='。阅读更多关于[功能隐藏](http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9)的信息,这应该能让你很好地了解相同情况的适用情况。 – 2012-02-17 07:04:53