You are passing
v
by value. MOreover, you are accessing its (copy) items (
v[i]
without know if they actually exists.
Your code should be more similar to
vector< Base * > Function()
ifstream file("example.txt");
vector < Base * > v{};
while ( file )
{
v.push_back( new Sub1(file) );
}
return v;
Note that even such code is dangerous: you have to explicitely delete the heap allocated objects.
Consider using smart pointers instead.