Guys, i am confused.
On some pages i find people saying that i MUST NOT inherit from std::vector, but after writing some lines of code for a class i realised that this is actually just a std::vector with a variable...
I'll just dump some code...
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
|
template <typename T>
class Tree
{
private:
class Node : public Tree<T>
{
public:
Node() {}
Node(T val) : mValue(val) {}
void SetValue(T val) { mValue = val; }
T GetValue() const { return mValue; }
private:
T mValue;
};
public:
Tree() {}
Tree(T value) { mNodes.push_back(Node(value)); }
const Node& operator[](unsigned int pos) const { return mNodes[pos]; }
unsigned int size() const { return mNodes.size(); }
void insert(T NewTree, unsigned int pos) { mNodes.insert(pos, Node(NewTree)); }
void push_back(T NewTree) { mNodes.push_back(NewTree); }
void erase(unsigned int pos) { mNodes.erase(pos); }
private:
std::vector<Node> mNodes;
};
|
this is basically just a std::vector of nodes, and nodes are a std::vector of nodes with a value, right?
I'd just like to do something like that now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
template <typename T>
class Node : public std::vector<Node<T>>
{
public:
Node() {}
Node(T val) : mValue(val) {}
void SetValue(T val) { mValue = val; }
T GetValue() const { return mValue; }
private:
T mValue;
};
template <typename T>
using Tree = std::vector<Node<T>>;
|
edit: found the correct syntaxes
Is it allowed to inherit from std::vector?
If no, what is the right way to do it?
edit 2: Is it possible to hide the implementation of Node? i tried a few different things but compiler says no
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
template <typename T>
class Tree : public Tree::Node<T>
{
class Node : public std::vector<Node<T>>
{
public:
Node() {}
Node(T val) : mValue(val) {}
void SetValue(T val) { mValue = val; }
T GetValue() const { return mValue; }
private:
T mValue;
};
};
|
if it is not possible, how can i safely hide the Implementation?