只是你忘记设置公共方法和变量
class Number:public Element, public Setupable
{
public:
Number(): Element(), Setupable() {}
vector<Digit> digit_vector;
};
顺便说一句,拥有一个成员变量 public 并不总是好主意,您应该提供一个返回 digit_vector 的方法
//the method is provided in 2 soups, and there are reson for that
//even if you can omitt the "const" version, is better you keep both
const vector<Digit> & getDigits() const { return digit_vector; }
vector<Digit> & getDigits() { return digit_vector; }
或者如果你的类必须只是一个“数据容器”,让它成为一个结构
struct Number:public Element, public Setupable
{
//default construsctors of Element and Setupable are still called!
//if not providing a default constructor, the compiler will create one
//automatically calling all default constructor (including the vector's
//one. Also digit_vector is constructed.
vector<Digit> digit_vector;
};
struct 与 class 相同,唯一的区别是默认情况下成员都是公共的,因此您不必指定“public:”。一个好的风格是只使用类(你必须记住必须是公共的还是私有的),并且只使用结构,你有一个几乎没有行为/逻辑的数据容器。
所以写:
class Number:public Element, public Setupable
{
//default construsctors of Element and Setupable are still called!
//if not providing a default constructor, the compiler will create one
//automatically calling all default constructor (including the vector's
//one. Also digit_vector is constructed.
public:
vector<Digit> digit_vector;
};
也是有效的。但通常你不希望隐式调用构造函数。