How to initialize static base type to a derived type?

Is there a way to do this?:
Base classes are in a library that the user can not edit.
The library has a class containing a static variable; the static variable is a base type object.
The user derives a class from the base class and initialize the static variable to a user derived type object.

Here is a simplified example of what I am trying to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

class numBase				//numBase is in a library user can not edit
{
};

class numDer : public numBase		//user defined class
{
	private:
		int num;
	public:
		void printNum() { std::cout << " numDer=" << num; }
		void inc() { num++; }
};

class containerBase			//containerBase is in a library user can not edit
{
	protected:
		static numDer count;	//this compiles, but user can not initilize count to other types
		//static numBase count;	//this causes error on line 26
};

class containerDerived : public containerBase //user defined class
{
	public:
		void inc() { count.inc(); } //error: 'class numBase' has no member named 'inc'
		void printCount() { std::cout << " containerDerived"; count.printNum(); }
};

/************************ user program **********************/
//initilialize static variable
numDer number;				//number could be any user defined type derived from numBase
numDer containerBase::count = number;	//initialize count to user defined type

int main()
{
	containerDerived container1;
	containerDerived container2;

	container1.printCount();
	container1.inc();
	container2.inc();
	container1.printCount();


output:
 containerDerived numDer=0 containerDerived numDer=2


Thank you.
If it was possible to static count, then using it in the derived class would be meaningless. Because, what is the guarantee that the static member is initialized.

Aceix.
Hi Aceix.

The compiler would check line 20 (if it were uncommented) to see if static count was initialize to a kind of numbBase, which it is on line 33.
That would guarantee that the static member is initialized.

How is using static count in the derived class would be meaningless?
Topic archived. No new replies allowed.