【发布时间】:2011-11-14 17:03:18
【问题描述】:
我制作了一个包含两个类的程序。基类包括其派生类的指针对象。然后我在基类的构造函数中初始化指针对象。
我的编译器在编译过程中没有给我错误,但是当控制台窗口出现时程序崩溃,派生类的对象出现UNHANDLED EXCEPION BAD ALLOCATION的错误。我该怎么做才能解决它?
代码如下:
class x;
class y
{
private:
x *objx; // here is the error
...........................
};
class x: public y
{
...........................
................
};
y::y()
{
objx=new x(); // bad allocation and the program crashes
// I have also tried this way by commenting objx=new x();
*objx=0; // but still the program crashes.
}
【问题讨论】:
-
请修正您的代码缩进。
-
这是什么语言?你应该相应地标记它。
标签: c++ memory object unhandled-exception