logo
down
shadow

How to clone class with vector of unique_ptr to base class


How to clone class with vector of unique_ptr to base class

By : John Seiver
Date : November 24 2020, 04:01 AM
I wish this help you The compiler cannot generate a copy constructor, because the vector member is non-copyable. If you wish to support copying, you need to implement it yourself, for instance:
code :
List(List const& other) : m_elements(other.m_elements.size()) {
    std::transform(begin(other.m_elements), end(other.m_elements), begin(m_elements),
      [](std::unique_ptr<IOutput> const& o_ptr) {
        return o_ptr->clone();
      }
    );
}


Share : facebook icon twitter icon
unique_ptr to a derived class as an argument to a function that takes a unique_ptr to a base class

unique_ptr to a derived class as an argument to a function that takes a unique_ptr to a base class


By : Rene Arends
Date : March 29 2020, 07:55 AM
around this issue I'm trying to use a unique_ptr to derived class in a function that takes a unique_ptr to a base class. Something like: , You have three options:
code :
f(std::move(derived));
void f(std::unique_ptr<Derived> const &);
std::unique_ptr<base> derived = std::unique_ptr<Derived>(new Derived);
std::unique_ptr<base> derived(new Derived);
std::unique_ptr<base> derived = std::make_unique<Derived>();
void f(Base & b);

f(*derived);
Assign derived class unique_ptr to base class unique_ptr

Assign derived class unique_ptr to base class unique_ptr


By : Nicolas Weninger
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I created a custom istream derived from std::istream that uses a custom streambuf when the file is a zipped file and std::filebuf otherwise. , Firstly, after this line
code :
std::unique_ptr<std::filebuf> fb;
fb->open(path.c_str(), std::ios::in);
auto fb = std::make_unique<std::filebuf>();
b_ = fb;
b_ = std::move(fb)
std::unique_ptr<std::streambuf> createStream(const std::string &path) {
    if(path.substr(path.length()-5, path.length())==".gzip"){  // I think you meant 5 here!
        return std::make_unique<gzipbuf>(path); 
    }
    auto fb = std::make_unique<std::filebuf>();
    fb->open(path.c_str(), std::ios::in);
    return fb;
}
my_stream::my_stream(const std::string &path) : std::istream(nullptr),
  b_(createStream(path)) {
    this->init(b_.get());
}
Instantiate unique_ptr to derived class from vector of base class where derived class has been stored

Instantiate unique_ptr to derived class from vector of base class where derived class has been stored


By : user2181655
Date : November 11 2020, 04:01 AM
this one helps. Consider following piece of code: , You don't want ownership transfer, so cast only the pointer:
code :
auto& banana = dynamic_cast<Banana&>(*product[0]);
auto c = b->color;
vector< unique_ptr<T> > as base class member

vector< unique_ptr<T> > as base class member


By : Ihor Slobodianyk
Date : March 29 2020, 07:55 AM
seems to work fine From the linked code in your comment, the problem is that you're trying to initialize the base class field directly in the derived class. You can't do that (and it makes no sense to try) as the base class fields are initialized by the base class constructor. If you want to initialize the field (rather than assigning to it), you need to do it in the base class constructor:
Add unique_ptr of derived class to vector of base class unique_ptr

Add unique_ptr of derived class to vector of base class unique_ptr


By : Endris Gtachew Ayale
Date : March 29 2020, 07:55 AM
I hope this helps you . unique_ptr guarantees that it's only one pointer that points to the memory, so you can't just copy it to the vector, you need to move it:
Related Posts Related Posts :
  • C++ on Vistual Studio with CMake error: manifest 'build.ninja' still dirty after 100 tries
  • Is there a way to automatically resolve an overloaded method via a template parameter?
  • Fastest way to find pair in a vector, remove it while iterating
  • error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'
  • Unable to call boost::clear_vertex while using listS for the vertex and edge lists
  • Decoding binary data from serial port
  • Pattern to Implement an OO interface to a C program written in an imperative style
  • CEPH + Librados++ is not returning consisten results
  • `LoadLibraryExW` triggers exception `0xC0000023` from `NtMapViewOfSection`
  • static_cast to a struct type to access all of its member variable
  • Forward declaration for boost::intrusive_ptr class to reduce compile time
  • How to use priority_queue with a non-static compare method of class instance?
  • Template parameters inside and outside class/struct
  • Determining prime number
  • How to resolve ambiguity between constructors taking std::string and std::vector
  • My program crashes when I try to change private values from an object
  • Unordered_map with custom class as key
  • Strict aliasing rules broken with templates and inheritance
  • C++ Derived Class Override Return Type
  • singly linked list c++ constructor, destructor and printing out
  • error: no match for operator
  • std::vector doesnt accept my struct as template
  • selection of people's contours
  • how to fix the (Error using mexOpenCV) on matlab?
  • Is or was there a proposal for c++ to use the context for short enum values?
  • Fair assumptions about std::hash implementations
  • undefined reference to libusb using cyusb
  • Function returns null pointer instead of address
  • C++17 copy elision and object destruction
  • Input multiple strings via operator>> c++
  • Avoiding overflow boost container
  • How to Write a Lambda Wrapping a Function with Optional Return Value
  • Partial specialization with more template parameters
  • How to convert fixed size array to pointer on pointer array
  • Memory leak in const member constructor with tag dispatching
  • C++ function with a generic and optional class
  • Custom QGraphicsItem That Contains Child QGraphicsItems
  • Are There Restrictions on What can be Passed to auto Template Parameters?
  • Rotating line inside rectangle bounds
  • Why do I need dynamic memory allocation if I can just create an array?
  • How can I convert a text file into a form that MPI_Bcast can send?
  • How to get array of all `this` of an instance
  • Using pointers as parameters
  • Automatic type deduction with const_cast is not working
  • Why does std::is_rvalue_reference not do what it is advertised to do?
  • Function Template Specialization with Forward Declared Type
  • template deduction failed in vector
  • Is there a signed `sizeof` alternative in Qt
  • clarification on overloading the -> operator
  • What is __m128d?
  • QtConcurrent: why releaseThread and reserveThread cause deadlock?
  • Function receiving different value than passed
  • Can C++ close a '''fstream''' variable after '''.close()'''?
  • Is it necessary to overload operator in this specific case
  • Comparing an element of a string array with a string
  • how to dereference a pointer of a map of pointers to objects in c++
  • How recursive function control flow change for static variable?
  • SDL 2 blitting BMP File
  • Why does an extremely large value cause this code to repeat infinitely?
  • Function returning different value than what is in function body before return statement
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org