所以我到处搜索,似乎找不到这个特定问题的答案。我正在使用带有 cygwin 和 gcc 3.4.4 cygming special 的 winXP。
问题:我有一个作为接口工作的类,它带有一些抽象方法和受保护的变量,它们应该存在于从该类继承的每个类中。现在我还有另一个类,它是这个接口的成员变量。
class Bar {
private:
int y;
public:
Bar(int why);
};
Bar::Bar(int why) : y(why) {}
class Foo {
protected:
Bar b;
public:
Foo(int x);
virtual void print_base();
};
Foo::Foo(int x) : b(x+3) // Have to use initializer list here.
{
//this->b(x+3); // doesn't work
}
class DerFoo : public Foo {
protected:
Bar db;
public:
DerFoo(int x);
};
DerFoo::DerFoo(int x) : Foo(x),
db(x+3) // (Bar)(int) being called, works fine
// db(4.0, 30) // no matching function for call to Bar::Bar(double, int)
// note: candidates are Bar::Bar(const Bar&), Bar::Bar(int)
// b(x-3) // doesn't work class DerFoo does not have any field named 'b'
{
//this->b(x - 3); // Doesn't work, error no match for call to (Bar)(int)
//this->db(x + 3); // Doesn't work, error no match for call to (Bar)(int)
}
所以你可以看到的问题是在派生的 foo 类内部,DerFoo 如何初始化 b。我已经尝试过成员初始化方法,但是编译器没有意识到受保护的变量。那么由于我不知道的一些奇怪的原因,它在这个类中找不到构造函数。即使包含对受保护成员变量(非继承)的构造函数的“错误”调用,它也会建议构造函数的正确版本。
我仍然不知道如何做到这一点。任何帮助是极大的赞赏。