I am trying to store an assortment of objects, all derived from a base type, in a vector. I had had it so that the vector was a std::vector<Base>, but realized that the type of the object was being sliced trying to copy it as a Base class. I am now trying to make it an std::vector<Base &> or std::vector<Base *> but I am having a dilema with things going out of scope, and forcing the user to use new. What I ideally want it something like this:

Code:
class Foo {
public:
    virtual void foo() { std::cout << "foo" << std::endl; }
};

class Bar : public Foo {
public:
    virtual void foo() { std::cout << "bar" << std::endl; }
};


Foo * test(Foo & f)
{
    return new Foo(f);
}


int main()
{
    Bar b;
    Foo * f = test(b);
    
    f->foo();
    delete f;
}
I would want f->foo() to print 'bar' instead of 'foo', retaining it's type information after operator new is applied, but such is not the case. Is there any way I can do this, or do I just have to force clients to use operator new when adding things to the class? Is this a reasonable precondition to force on a client?