c++ - Passing inherited class type as argument -


Problems passing through the type of class inherited in the form of logic for the method that takes me to class squared has occurred.

  class base {...} Category derivative: public base {...} square container {vector & lt; Base * & gt; remaining part; // 1 public: zero addToCont (base x) {// 2 cont.push_back (& ​​amp; x); }} Int main () {Container C; c.addToCont (Derivative (P1, P2)); // 3}   

1) I think I need the containers of points for objects to work

2) Error in conversion from derivative to base

3) I do not want to change this call. I tried

  derived D (P1, P2); C.addToCont (d);   

with

  addToCont (base and x)   

and it worked for me.

My problem is that I have 3 derivative classes and I do not want to overload the add method 3 times. I think I have to add some virtual pattern or some type of structure to those classes, but I did not find anything about it I'm a newbie in heritage and I am very confused about this. Thank you for all of your help.

Some notes:

  1. Must use pointers in Base A vector of so that you can control the objects from the hierarchy. It goes without saying that you are probably using some kind of smart pointers instead of raw pointers, but that goes in the preferences and how much you like the risk
  2. Zero addToCont (base x) is incorrect, even if you are only adding a base object, you will add a pointer to a local variable (pass-by-value parameter)
  3. Using zero addToCont (Base and X) the way you want to place it As with the previous derived D , due to the earlier, as soon as D exits from the scope, you are left with a false indicator stored in the pointer
  4. Calling addToCont (Derived (...)) object passes through a temporary. When you think about managing your memory then it should be kept in mind.
  5. Why do not you see the need to overload addToCont for all Derivative classes, it's your zero addToCont (base & x)
  6. solution (If you live on raw pointers) Zero addToCont (base * x) There you can pass an indicator on the base or any derivative again, you keep in mind about memory management needed. You may need to allocate derivative objects with new derivative (...) and you have to see who owns it, and who is responsible for removing it (for example, When the container object is destroyed).
  7. You should remember that virtual to make base descent, because you will destroy the object derived from the base pointers, and if the destroyer is not virtual, then the object will only be partially destroyed. .

    If the addToCont (derived (...)) call is absolutely necessary, then you zero addToCont (base &; x) Definition .... but they should clone the vector before inserting it into the vector: Redo addToCont (Const. Base and X) {// cont.push_back (x.clone ()) ; }

    But then .. you need a virtual base * clone () const (at least) in derivative orbits, which is a derivative Base pointer with an exact copy of the object, which includes additional copies of the items and additional cloning ...

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -