Looking to do the impossible, or hopefully the improbable...

I have been a c++ programmer for many years now, but someone recently asked me to do something I honestly do not think is possible to do, but I figured I would at least ask the community before saying it was or not. Here is the long short of it.

It starts with a virtual "node" class that will be used to create many different derived classes, for example "NodeA, NodeB, NodeC, etc". There will also be a "NodeController" class that will be used as an interface for creating/destroying all classes derived from "Node". Basically all "Node" classes will have private constructors, and private virtual destructors. And "NodeController" will be a friend class of each to insure that it is the only class capable of creating or destroying them. If that makes sense? So in the end we are able to do things like this.

/////////////////////////////////////////////
#include "Node.h"
#include "NodeA.h"
#include "NodeB.h"
#include "NodeController.h"

int main()
{
//NodeA* nodeA = new NodeA(); :NOT ALLOWED: - Private constructor!
//NodeB* nodeB = new NodeB(); :NOT ALLOWED: - Private constructor!

NodeA* nodeA = nodeController.createNodeA(); //Creates pointer, new, return
NodeB* nodeB = nodeController.createNodeB(); //Creates pointer, new, return

nodeA->doSomething();
nodeB->doSomething();

nodeController.destroyNode(nodeA); //Takes pointer, delete, return
nodeController.destroyNode(nodeB); //Takes pointer, delete, return
}

/////////////////////////////////////////////

So obviously I can create a function inside of "NodeController" for each type of "Node" in order to create it "createNodeA, createNodeB, etc", and since the destructors of the "Node" classes are virtual, we only need one destroyNode accepting a "Node" pointer. I hope I havent lost you thus far... There is a problem here though. I want others to be able to derive from classes that have been derived from "Node", and allow them to be used by the controller as well. And its obviously very hard to define a "create" function for a node that you have not defined yet. What I would like to do is something more like a macro that will allow me to create ANY type of "Node" and return an instance of it from the "NodeController". Something like.

NodeA* nodeA = nodeController.createNode(NodeA);
NodeB* nodeB = nodeController.createNode(NodeB);
NodeAExtended* nodeAExtended = nodeController.createNode(NodeAExtended); // Did not exist originally

I know obviously that wont work, but does anyone know if through macros or something obscure in the land of C++ if there is a way to basically pass the type as a parameter in order to dynamically return an object of that type?

Sounds like you want to use templates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Node {};

struct NodeController
{
	template <class T>
	T* createNode()
	{
		return new T;
	}
};

class NodeExtended : public Node {};

int main()
{
	NodeController nodeController;
	NodeExtended* nodeAExtended = nodeController.createNode<NodeExtended>();
}
Thank you. I must admit, even though I have been writing in c++ for many years, I have never once actually needed a reason to make a template, and I completely forgot they existed. Which is ironic since I use STL every day. ;-).

Your solution will work perfectly, but instead of:

return new T;

I will be using:

1
2
3
4
5
6
7
8
9
10
11
T* derived = new T;
if (dynamic_cast<Node*>(derived)) //Verify derived is a subclass of Node!
{
     return derived;
}

else
{
     delete derived;
     return NULL;
}


That will insure that only classes derived from Node will actually be returned. Again, huge thank you.
Last edited on
Topic archived. No new replies allowed.