Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base class 'class std::vector<...>' has a non-virtual destructor

One of my C++ classes derives from std::vector so that it can act as a container that also perform custom actions on its content. Unfortunately, the compiler complains about the destructor not to be virtual, which I cannot change, since its in the standard library.

Am I doing the whole thing wrong (thou shall not derive from STL) or is there something I could do to keep the compiler happy ? (appart from stop using -Weffc++ :)

edit: the derived class do not touch the vector-manipulation algorithms, but merely add some information such as "element width/height" for a vector of images. As an example, you could think of

class PhotoAlbum: public std::vector<Photo> {
    String title;
    Date from_time, to_time;
    // accessors for title and dates
    void renderCover(Drawable &surface);
};

where you think of a photo album primarily as a collection of pictures with some meta-data (title and time) and album-specific features such as rendering a thumbnail of some Photo onto a surface to make the album cover. So imho, the photo album IS-A collection of Photo, more than it HAS-A such collection.

I fail to see any benefit I'd gain of having getPhotoVector() method in a PhotoAlbum that would have an extra "collection" field.

like image 364
PypeBros Avatar asked Aug 04 '26 11:08

PypeBros


1 Answers

Why not use composition? Simply make std::vector a member of your custom container, then implement the custom actions as member functions of said class that act upon the std::vector member. That way, you have complete control over it. Besides, you should prefer composition over inheritance if inheritance is not required.

like image 165
In silico Avatar answered Aug 07 '26 00:08

In silico