Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stl vector.push_back() abstract class doesn't compile

Let's say I have an stl vector containing class type "xx". xx is abstract. I have run into the issue where the compiler won't let me "instantiate" when i do something like the following:

std::vector<xx> victor;
void pusher(xx& thing)
{
    victor.push_back(thing);
}

void main()
{
    ;
}

I assume this is because the copy constructor must be called. I have gotten around this issue by storing xx*'s in the vector rather than xx's. Is there a better solution? What is it?


1 Answers

When you use push_back, you are making a copy of the object and storing it in the vector. As you surmised, this doesn't work since you can't instantiate an abstract class, which is basically what the copy-construction is doing.

Using a pointer is recommended, or one of the many smart-pointer types available in libraries like boost and loki.

like image 155
Steve Guidi Avatar answered Aug 06 '26 03:08

Steve Guidi