【发布时间】:2011-09-05 10:23:30
【问题描述】:
如何从子方法访问基类变量?我遇到了分段错误。
class Base
{
public:
Base();
int a;
};
class Child : public Base
{
public:
void foo();
};
Child::Child() :Base(){
void Child::foo(){
int b = a; //here throws segmentation fault
}
在另一个班级:
Child *child = new Child();
child->foo();
【问题讨论】:
-
这段代码不会编译。
-
请修复你的代码,很明显, foo 方法是 Child::foo 的实现。
-
投票结束这个问题有点苛刻,给 OP 修复代码的机会,似乎是一个有效的问题......
-
foo 被声明为私有方法。我想知道你是怎么称呼它的?
-
好吧,你对这个编辑没有多大帮助。看起来仍然不是有效的代码。
标签: c++ inheritance segmentation-fault