Eine Operation Die Klasse enthält einen Enum, der die boolesche Operation darstellt, um sie für seine std :: vector operands auszuführen. Diese bestehen die Zweige im Baum. Diese bilden die Blätter im Baum. Sowohl ein Operation als auch eine Bedingung können das Root des Baumes sein. Ist es ein inhärenter Teil meines Problems zu haben, diese Kopiekonstruktoren zu haben?
Code: Select all
#include
#include
#include
#include
namespace tree
{
struct Node
{
// Virtual clone function to make a new std::unique_ptr
// with a copy of the contents of the old std::unique_ptr
// Both std::unique_ptrs will be independent of each other
virtual std::unique_ptr clone() const = 0;
std::string name;
};
enum class Boolean
{
And,
Or,
Xor
};
struct Operation : public Node
{
// Copy constructor (because copying a std::vector is non-trivial)
// that will allocate a new std::vector of the same size and clone
// new std::unique_ptrs from the other std::vector
Operation(const Operation &other) : op(other.op), nodes(other.nodes.size())
{
std::transform(other.nodes.cbegin(), other.nodes.cend(), nodes.begin(),
[](const auto &old) {
// This line has the compilation error because Node is abstract,
// but I need this for the polymorphism, correct?
return op ? std::make_unique(old->clone()) : nullptr;
});
}
// Clones this class
virtual std::unique_ptr clone() const override
{
return std::make_unique(*this);
}
Boolean op;
// Can hold any number of other Operation or Condition objects
std::vector nodes;
};
struct Condition : public Node
{
// Clones this class
virtual std::unique_ptr clone() const override
{
return std::make_unique(*this);
}
int datum;
};
struct Event
{
Event(std::string name) : name(name) {}
// This line has the same compilation error
Event(const Event &other) : name(other.name), root(std::make_unique(other.root->clone())) {}
std::string name;
std::unique_ptr root;
};
} // tree