1

If my class has:

    std::vector<std::shared_ptr<Animal>> table;

While Animal is an abstract class that is inherited by others, will the default d'tor be enough?

I know that it will be enough for the vector itself but what about what is contained inside that vector?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • To be clear, we are talking about the destriuctor for the class that contains `table`, and not the destructor for `Animal`, yes? – user4581301 Jun 25 '20 at 21:16
  • Don't overload questions by tacking on unrelated new questions please. `table` will be initialized with `size` default initialized `shared_ptr`s. – user4581301 Jun 25 '20 at 21:18
  • yes @user4581301 –  Jun 25 '20 at 21:19
  • what do you mean by "will be initialized with size default initialized shared_ptrs" how may I know what's the default? I searched google and nothing found –  Jun 25 '20 at 21:20
  • @BigSur The default is the shared pointer equivalent of a nullptr https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr, as it says in the link 'constructs a shared_ptr with no managed object'. – john Jun 25 '20 at 21:22
  • 1
    I rolled back your question. There should be only one question asked per post, and you shouldn't change your question after you received an answer. – Modus Tollens Jun 25 '20 at 21:26

1 Answers1

2

You can use a virtual destructor in Animal as a matter of good practise:

virtual ~Animal(){}

Though a virtual destructor is not really needed to properly use polymorphism when using shared_ptr.

Credit to @NathanOliver's comment.

You don't need to worry about memory management in this instance.

anastaciu
  • 23,467
  • 7
  • 28
  • 53