mardi 24 mars 2015

Create generic Container class in C++


I want to, in my C++ application, define a class in which one of the member object is a "container", a certain data structure that could support general operations like insert(), lookup() etc.


Essentially I am designing a class that looks like this:



class LiteralNode:public ExprNode {
private:
Container container;
String type;
public:
bool insert();
bool lookup();
};


So this is part of the leaf node of an expression tree, where this specific node is supposed to contain a certain data structure storing incoming data. Depending on the property of the data, it might either be say, HashMap, or BinarySearchTree. But even with HashMap, it should be strong typed with the data type stored. The actual choice will be made when the first piece of data comes in, and I will choose and instantiate the actual container class by inspecting the data type.


But the class itself is supposed to be generic and universal for all tress with all types of incoming data, thus the requirement.


I'm new to C++ and likewise new to concepts like templates. I read some basic template docs and am also aware of the "algorithm" header in C++ that includes methods like inserter, but still not sure if I'm looking at the right thing. I also saw this post where the OP seems proposing exactly something I have in mind, however, I don't understand the answers on that question and not sure why people in the comments saying "don't do that"...


How can I achieve what I want, or, is there something wrong with this kind of design thoughts? Thanks!




Aucun commentaire:

Enregistrer un commentaire