Initializing static map of variable type abstract class

The Situation:
A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:

1
2
3
4
5
6
7
8
9
//PARENT.H
class Parent
{
protected:
     static int n;
     static std::map<int, Parent*> map;
public:
     virtual void pureVirtual() = 0;
}

1
2
3
4
5
6
7
8
9
10
//PARENT.CPP
//Initialize static int
int Parent::n = 0;
//Initialize static map
std::map<int, Parent*> Parent::map = std::make_pair(-1, new Child);     //THIS IS WHERE PROBLEMS ARISE

Parent::Parent()
{
     n++;
}

1
2
3
4
5
6
7
//CHILD.H
class Child : public Parent
{
public:
     Child();
     void pureVirtual();
}

1
2
3
4
5
6
7
8
9
10
11
//CHILD.CPP
Child::Child()
{
     //Add this object to the static map
     map[n] = this;
}

void Child::purevirtual()
{
//Does nothing
}


The Problem:
In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.
I also can't initialize it as new Parent, seeing as the parent class is an abstract one.

The Question:
Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option. Neither is making the map not static, though if there are some alternative approaches to the situation, do let me know.
Include the child header in the parent source file, not in the parent header.

Why do you need to initially have a child object in the map?

Also, you should prefer to use some smart pointer wrapper class instead of raw pointers.
Last edited on
Include the child header in the parent source file, not in the parent header.

Didn't work entirely. I had to make an additional method that returned a static map with an element initialized, and made the class variable equal to that method.

Why do you need to initially have a child object in the map?

Like I said, the Parent class is abstract and I need it to remain that way. I need to initially have a child object in the map because I don't see any other choice

Also, you should prefer to use some smart pointer wrapper class instead of raw pointers.

How do you suggest I make use of smart pointers in a situation like this?
Last edited on
Food 4 Thought wrote:
Like I said, the Parent class is abstract and I need it to remain that way. I need to initially have a child object in the map because I don't see any other choice
This is not an explanation. Why don't you have any other choice?
Food 4 Thought wrote:
How do you suggest I make use of smart pointers in a situation like this?
I can't help you build your car if you only tell me what you want the front right tire to look like.
Topic archived. No new replies allowed.