I'm stuck using this pattern, because only one of the derived classes i have created gets instantiated. Checked with g++ and MSVS. Specifically only the first derived class I define gets created. No warnings of any kind are emitted by the compiler. Complete code is provided below.
#include <iostream>
static int nodes = 0;
class TreeNode {
private:
int m_id;
public:
TreeNode() :
m_id(++nodes)
{}
TreeNode(int id) :
m_id(id)
{
++nodes;
}
TreeNode* left;
TreeNode* right;
int getId() const {
return m_id;
}
};
template<typename T>
//typename std::enable_if<std::is_base_of<TreeParser, T>::value>::type
class TreeParser {
protected:
TreeParser() {
++parsers;
}
public:
static uint32_t parsers;
void preorderTraversal(TreeNode* node) {
if (node != nullptr) {
processNode(node);
preorderTraversal(node->left);
preorderTraversal(node->right);
}
}
virtual ~TreeParser() = default;
void processNode(TreeNode* node) { // 2, 3. the generic algorithm is customized by derived classes
static_cast<T*>(this)->processNode(node); // depending on the client's demand - the right function will be called
}
};
template<class T>
uint32_t TreeParser<T>::parsers = 0;
class SpecializedTreeParser1 : public TreeParser<SpecializedTreeParser1> // 1. is-a relationship
{
public:
explicit SpecializedTreeParser1() :
TreeParser()
{}
void processNode(TreeNode* node) {
std::cout << "Customized (derived - SpecializedTreeParser1) processNode(node) - "
"id=" << node->getId() << '\n';
}
};
class SpecializedTreeParser2 : public TreeParser<SpecializedTreeParser2> // 1. is-a relationship
{
public:
explicit SpecializedTreeParser2() :
TreeParser()
{}
void processNode(TreeNode* node) {
std::cout << "Customized (derived - SpecializedTreeParser2) processNode(node) - "
"id=" << node->getId() << '\n';
}
};
int main()
{
TreeNode root;
TreeNode leftChild;
TreeNode rightChild;
root.left = &leftChild;
root.right = &rightChild;
std::cout << "Root id: " << root.getId() << '\n';
std::cout << "Left child id: " << leftChild.getId() << '\n';
std::cout << "Right child id: " << rightChild.getId() << '\n';
SpecializedTreeParser1 _1;
_1.preorderTraversal(&root);
SpecializedTreeParser2 _2;
_2.preorderTraversal(&root);
}
The output is:
Root id: 1
Left child id: 2
Right child id: 3
Customized (derived - SpecializedTreeParser1) preorderTraversal() - id=1
Customized (derived - SpecializedTreeParser1) preorderTraversal() - id=2
Customized (derived - SpecializedTreeParser1) preorderTraversal() - id=1963060099 // what is that?
Why can't I instantiate the second derived class?
I I'm not mistaken, your program has undefined behavior. The left
and right
pointers of leftChild
and rightChild
are left uninitialized, so as soon as preorderTraversal()
gets there, your program blows up. That's also why you get the weird id
in the end: it's reading from some random memory location…
To solve this problem, make sure the left
and right
members of a TreeNode
are always initialized to nullptr
as the rest of your code seems to expect:
class TreeNode {
…
TreeNode* left = nullptr;
TreeNode* right = nullptr;
…
};