I've been out of the C++ game for a while and now I'm suddenly blanking on basic concepts. I've got an ordered list of classes that each store the same things as the previous class in the list, plus one additional thing. I don't want each class to have to write out the things that the previous classes do again, and I want to be able to store all of them in an array of pointers to any of the previous classes. The things that they store should be const because they never change after the class is instantiated. I thought it would be this:
class A {
protected:
constint _a;
public:
A(int a = 0) : _a(a) {};
constint getA() const { return _a; }
};
class B : public A {
protected:
constint _b;
public:
B(int a = 0, int b = 0) : _a(a), _b(b) {};
constint getB() const { return _b; }
};
class C : public B {
protected:
constint _c;
public:
C(int a = 0, int b = 0, int c = 0) : _a(a), _b(b), _c(c) {};
constint getC() const { return _c; }
};
But this gives me the errors "'_a' is not a nonstatic data member or base class of class 'B'", "'_a' is not a nonstatic data member or base class of class 'C'", and "'_b' is not a nonstatic data member or base class of class 'C'". So, how do I do this?
class A {
protected:
constint _a;
public:
A(int a = 0) : _a(a) {}
constint getA() const { return _a; }
};
class B : public A {
protected:
constint _b;
public:
B(int a = 0, int b = 0) : A(a), _b(b) {}
constint getB() const { return _b; }
};
class C : public B {
protected:
constint _c;
public:
C(int a = 0, int b = 0, int c = 0) : B(a,b), _c(c) {}
constint getC() const { return _c; }
};
You want to initialize the class inherited from, not the individual members of those classes.