Quote:
"The virtual dispatch mechanism happens only through pointers." ...and references
Very true, I should have pointed that out.
Quote:
"The virtual dispatch mechanism happens only through pointers." ...and references
Quote:Original post by Promit
The previous poster mentioned it, and I'd like to emphasize it.
You must avoid assigning a derived class to a base class by value.
When this happens, it's called slicing, because any data members of the derived class with be sliced off, and only the base class will be copied. Virtuality functions only through pointers and references. Be extremely careful when working with base class values; don't copy them as the base class, don't pass them by value to functions, don't insert them by value into containers, etc. Your omg vector is dangerous, and inserting a Test2 into it is a mistake. All of the information about Test2 is destroyed when you do that.
Oh, and if you want to attempt Zahlman's solution, be very careful. STL containers take some parameters by value instead of by const reference, so if your pointer wrapper's destructor deletes the pointer, you will always destroy your object when you attempt to add it to a container.
template <typename T> class vector<auto_ptr<T> > { int cant_make_container_of_auto_ptrs[0]; };
class Test2 : public Test1{ public: void x() { Test1::x(); std::cout << "2"; }};
Quote:Original post by Deyja
It doesn't, which is rather disapointing.
Quote:Original post by erissian
It seems like you want to have the child class perform the actions of the base class, plus other stuff? This works fine for me:class Test2 : public Test1{ public: void x() { Test1::x(); std::cout << "2"; }};