The following simple code shows that std::unique_ptrit works great in terms of polymorphism by typing "Hello from Derived.",
#include <iostream>
#include <memory>
using std::cout;
struct Base
{
virtual ~Base() { }
virtual void SayHello()
{
cout << "Hello from Base.\n";
}
};
struct Derived : public Base
{
void SayHello() override
{
cout << "Hello from Derived.\n";
}
};
int main()
{
std::unique_ptr<Base> pBase( new Derived() );
pBase->SayHello();
}
In any case, observing raw pointers is fine; what you should pay attention to is the presence of raw pointers. Owners of raw pointers should be safely wrapped in RAII boundaries (using unique_ptr, shared_ptror some kind of user resource manager).
source
share