Deriving from C++ STL classes?

In summary, it's an acceptable practice to derive from std::exception but tread carefully lest there be quicksand.
  • #1
sbrothy
Gold Member
446
342
I'm not a newbee to C++ but I've been out of the loop for a while.

I know it's discouraged, if not directly a no-go, to derive from STL classes. Something that has to do, if I remember correctly, with the STL classes having no virtual destructors.

It will work in the short run but may explode in your face later if you don't know exactly what you're doing.

With std::exception (and std::runtime_error), however, which does actually have virtual destructor(s) this practice should be OK though, right?

Regards.
 
Technology news on Phys.org
  • #2
  • Like
Likes Vanadium 50 and sbrothy
  • #3
I may have unintentionally muddied the waters here. I didn't mean deriving from STL classes (which I'm pretty sure is frowned upon) but from std::exception (or possibly std::runtime_error, but in my scenario I don't need the what() string that comes with the last). I want to store a std::vector (or some other STL container) inside my std::exception derivative because my custom exception needs to store a bunch of warnings or errors. It seems to be widely used and I can't really see any downsides which outweighs the potential benefits.

I can even override the ostream<< operator for easy output. For output to std::cerr and DEBUG output.

I'll post my std::exception derivative one I've cooked it up. You are welcome to shoot it down by then. :)
 
  • #4
@sbrothy, I don't see any downside to this. After all, there are already several specific exception subtypes that derive from exception class. These include the classes declared in the STL <stdexception> header, including logic_error, domain_error, runtime_error, range_error, and overflow_error.
 
  • Like
Likes sbrothy
  • #5
Good. As I pointed out I may have mixed up deriving from STL containers with deriving from std::exceptions. After all it looks like what the the std::exception is meant for. Virtual constructor and all. But as I mentioned I've been out of the look for a while so just wanted to be sure. I want to write a little C++ guide for people coming from C, so naturally I didn't want to put my foot in it right away. The way I want to derive from std::exception seems pretty "by design" and frankly I can see no better way.

I'll keep you updated...
 

1. What is the purpose of deriving from C++ STL classes?

Deriving from C++ STL (Standard Template Library) classes allows for the creation of custom classes that inherit the functionality and features of the base class. This allows for code reuse and makes it easier to create new classes with similar functionality.

2. Can any class be derived from an STL class?

Yes, any class can be derived from an STL class, as long as the base class is not marked as final, which would prevent it from being inherited from.

3. How do I derive from an STL class?

To derive from an STL class, use the class or struct keyword followed by the name of the new class, a colon, and the name of the STL class to be inherited from. For example: class MyVector : public std::vector<int>.

4. Can I override or add new functions to an inherited STL class?

Yes, you can override or add new functions to an inherited STL class. However, keep in mind that the base class may already have functions with the same name, so make sure to use the virtual keyword and the override specifier when overriding functions to ensure proper function calls.

5. Are there any special considerations when deriving from an STL class?

Yes, there are a few special considerations to keep in mind when deriving from an STL class. One is that the base class may have private member variables or functions that cannot be accessed or modified in the derived class. Another consideration is that the base class may have non-virtual functions, which can lead to unexpected behavior if overridden in the derived class. It is important to thoroughly understand the base class before deriving from it to avoid any issues.

Similar threads

  • Programming and Computer Science
Replies
19
Views
979
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
818
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
7K
Back
Top