c++: can vector contain objects of type Derived?
No, the Derived objects will be sliced: all additional members will be discarded. Instead of raw pointers, use std::vector<std::unique_ptr<Base> >.
No, the Derived objects will be sliced: all additional members will be discarded. Instead of raw pointers, use std::vector<std::unique_ptr<Base> >.
C++ references are intentionally not specified in the standard to be implemented using pointers. A reference is more like a “synonym” to a variable than a pointer to it. This semantics opens some possible optimizations for the compiler when it’s possible to realize that a pointer would be an overkill in some situations. A few … Read more
What you are seeing is Object Slicing. You are storing object of Derived class in an vector which is supposed to store objects of Base class, this leads to Object slicing and the derived class specific members of the object being stored get sliced off, thus the object stored in the vector just acts as … Read more
“Slicing” is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information – some of it is “sliced” away. For example, class A { int foo; }; class B : public A { int bar; }; So an object of type B has … Read more