Can't assign const base class members in derived class

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class A {
protected:
	const int _a;
public:
	A(int a = 0) : _a(a) {};
	const int getA() const { return _a; }
};

class B : public A {
protected:
	const int _b;
public:
	B(int a = 0, int b = 0) : _a(a), _b(b) {};
	const int getB() const { return _b; }
};

class C : public B {
protected:
	const int _c;
public:
	C(int a = 0, int b = 0, int c = 0) : _a(a), _b(b), _c(c) {};
	const int 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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class A {
protected:
	const int _a;
public:
	A(int a = 0) : _a(a) {}
	const int getA() const { return _a; }
};

class B : public A {
protected:
	const int _b;
public:
	B(int a = 0, int b = 0) : A(a), _b(b) {}
	const int getB() const { return _b; }
};

class C : public B {
protected:
	const int _c;
public:
	C(int a = 0, int b = 0, int c = 0) : B(a,b), _c(c) {}
	const int getC() const { return _c; }
};


You want to initialize the class inherited from, not the individual members of those classes.
Last edited on
Topic archived. No new replies allowed.