【发布时间】:2010-02-02 00:15:17
【问题描述】:
我在以下代码中通过 g++ 编译器遇到了一些令人讨厌的分段错误。关于为什么会发生这种情况以及如何解决它的任何想法都会很棒。
#include <iostream>
using namespace std;
class Base {
public:
Base() {}
virtual ~Base() {};
virtual int getNum(int) = 0;
};
class Derived: public Base {
public:
Derived() :
Base() {}
~Derived() {}
int getNum(int num) {
return num;
}
};
class Foo {
public:
Foo() {
};
void init() {
Derived n;
*baseId = n;
}
void otherStuff() {
cout << "The num is" << baseId->getNum(14) << baseId->getNum(15) << baseId->getNum(16) << baseId->getNum(15) << endl;
}
Derived* baseId;
};
int main() {
Foo f;
f.init();
f.otherStuff();
return 0;
}
【问题讨论】:
-
是因为你取消了
baseId的引用吗?
标签: c++ inheritance g++ segmentation-fault