c++ - unique_ptrs and converted pointers and a vector -


i created abstract class , created child classes inherit abstract class.

  class a{        public:        virtual a* clone() const = 0;        virtual a* create() const = 0;        ~virtual a(){};        // etc.    }; 

child classes

     class b: public a{};      class c: public a{}; 

i use implicit conversion of child class pointer base class pointer in main function.

     int main(){       b* pb = new b();      c* pc = new c();       a* pa = pb;     //converting pointer a, pointer b      a* paa = pc;    //converting pointer a, pointer c 

i can populate vector these classes using pointer of type , access child classes via polymorphism.

    vector<a*> pntr; 

however want each child class take responsibility of own memory release. know can use unique pointers this. question how implement this.

if code this:

    pa = unique_ptr<a>(pb);     paa = unique_ptr<a>(pc); 

then populate vector this:

    vector<unique_ptr<a>>pointers;     pointers.push_back(move(pa));     pointers.push_back(move(paa)); 

not sure work. , confused destroyed when vector goes out of scope. converted pointers pa , paa set null or class objects destroyed - original intention. clarity needed.

why don't try it?

the example below presents way in create interface (your abstract base class) , 2 derived classes implementing interface (foo , bar in case).

you can create container of std::unique_ptr<interface> (unique pointers abstract base class) , populate using various methods , helper functions: emplace_back std::make_unique, push_back std::move, , on.

after run example notice destructor gets called automatically --- that's beauty of std::unique_ptr.

#include <cassert> #include <memory> #include <vector> #include <iostream>  class interface {  public:   virtual ~interface() {     std::cout << "destroying object" << std::endl;   }   virtual void method() const = 0; };  class foo // foo "implements" or "offers access to" interface     : public interface {  public:   void method() const override { // here override interface::method     std::cout << "foo::method" << std::endl;   } };  class bar // bar "implements" or "offers access to" interface     : public interface {  public:   void method() const override { // here override interface::method     std::cout << "bar::method" << std::endl;   } };   int main() {   // declare   std::vector<std::unique_ptr<interface>> objects;    // populate   objects.emplace_back(std::make_unique<foo>());   objects.emplace_back(std::make_unique<bar>());   objects.emplace_back(std::make_unique<foo>());    // way populate   std::unique_ptr<foo> pfoo(new foo);   objects.push_back(std::move(pfoo));    // careful... pfoo points nullptr because   // moved ("transfered control of") pointer   // vector; pfoo no longer owns (so owns nothing;   // nullptr)   assert(pfoo == nullptr);    // can iterate on members reference (otherwise   // need make copies, , cannot copy unique_ptr)   for(const auto & pobject : objects) {     pobject->method();   }    // @ exit should see calls destructor    return 0;  } 

compile , run:

$ g++ example.com -std=c++14 -wall -wextra $ ./a.out foo::method bar::method foo::method foo::method destroying object destroying object destroying object destroying object 

Comments