We have a base class.....
class Node
{
public:
std::string Name;
Node(){};
~Node(){};
}
Filter is a derived class of Node. Filter is the same a node, except it has a 'filterId' property and it's constructor populates Node.Name='Filter'.
class Filter: public Node
{
public:
int filterId;
Filter() : Node()
{
Name="Filter";
}
}
I have a ClassFactory that returns std::shared_ptr<Node> when passed a string
Please find the code to that factory on CodeProject following this link.
http://www.codeproject.com/Articles/567242/AplusC-b-bplusObjectplusFactory.
The code seems to instantiate a Filter, but, returns a Node. i.e. Object slicing.
std::shared_ptr<Node> n = std::make_shared(Node); // As expected, doesn't not have filter ID and node.Name is blank.
std::shared_ptr<Filter> f = std::make_shared(Filter); // As expected, has filterID and node.Name is 'Filter'.
Now i want to handle an array of std::shared_ptr(Node) which may actually contain std::shared_ptr(Filter) depending on what the class is requested from the factory.
std::shared_ptr<Node> nf = std::make_shared(Filter); // Error. Does not have filterId, but does have **Node**.Name='Filter'.
I even tried the simpler case..... and got the same problem.
Node n = Node();
Filter f = Filter();
Node nf =Filter(); // Error, nf does not have a filterID, but it's Name field is set to 'Filter'.
What am i doing wrong here? The Node is a base class for all of my derived classes. When i have a function that accepts a Node, i should be able to send it a Filter and have the filterId available.
Pointers are advised to help in situations like this, however the shared_ptr is a pointy as things will get.