0

Right now I have a hierarchy as follows (-> means parent of )

LSystem -> DLSystem -> Tree -> MonopodialTree
                            -> TernaryTree

And I have a function substitute which is defined in LSystem as follows and given a minimal implementation that LSystem, DLSystem and Tree use.

virtual void substitute(Symbol s, std::vector<Symbol> &string);

Next, MonopodialTree and TernaryTree override the definition of substitute.

The problem arises because I want to have a std::vector<Tree> (with both Monopodial and Ternary) and iterate through it calling substitute for each one of them.

However, they both call LSystem's substitute function instead of their own substitute function, what am I missing?

  • Also see [this post](https://stackoverflow.com/questions/4403726/learning-c-polymorphism-and-slicing). Basically you need pointers (e.g. `std::vector`) to avoid "object slicing" and to allow polymorphic behavior. – Cory Kramer Nov 29 '15 at 21:20
  • But then it fails when I try MonopodialTree MT(); Tree* T = &MT; – Jose Javier Gonzalez Ortiz Nov 29 '15 at 21:30
  • Try instead `MonopodialTree MT{}; Tree* T = &MT;` (note the `{}` instead of `()`), otherwise you declared a function, see [most vexing parse](https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work) – Cory Kramer Nov 29 '15 at 21:34
  • Then I get -> terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc – Jose Javier Gonzalez Ortiz Nov 29 '15 at 21:42
  • With the code you posted I cannot help you debug your code. If you have a new question, please feel free to post a new question on Stack Overflow with a small but complete example and the full error message you are receiving. – Cory Kramer Nov 29 '15 at 21:57

0 Answers0