Jump to content
  • Advertisement
Sign in to follow this  
BigFreak

Extending the standard library?

This topic is 4386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good idea/bad idea? I want some custom functions for strings and file streams which aren't there already, so I was thinking of just inheriting out those classes. Would this work fine or is it not reccommended?

Share this post


Link to post
Share on other sites
Advertisement
Bad Idea!

The standard library containers are not meant for inheiritance. In particular, they do not have virtual destructors, so they will not be destroyed correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by BigFreak
Good idea/bad idea? I want some custom functions for strings and file streams which aren't there already, so I was thinking of just inheriting out those classes. Would this work fine or is it not reccommended?
However you go about it, before you implement these functions yourself be sure to check out boost::filesystem and the boost string algorithms and parsers; it's at least a possibility that what you're looking for already exists in these libraries.

If you do end up implementing the functions yourself, consider making them non-member functions (which, among other things, will avoid the inheritance problem).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, so it's a bad idea because the dtor is not declared virtual ... but what other reasons are there. I ask out of curiosity and because i could* always do this:


template <typename _Ty, typename _Ax = std::allocator<_Ty> >
class MyWayMoreAwesomeVector: public std::vector<_Ty, _Ax>
{
public:
typedef std::vector<_Ty, _Ax> parent;
~MyWayMoreAwesomeVector()
{
parent::~vector();
}
};

Share this post


Link to post
Share on other sites
Well, for a start, if you were pointing at your derived vector through a standard vector pointer when you deleted it, your derived destructor would not get called so any custom deallocation etc you were doing would be bypassed.

Containment is normally a better way to extend the standard library classes rather than inheritance.

[EDIT] Enigma has just pointed out a far more serious issue that I am ashamed to say I totally missed as well.

[Edited by - EasilyConfused on August 11, 2006 8:10:27 AM]

Share this post


Link to post
Share on other sites
AP: That code doesn't fix anything. In fact it breaks it further. As EasilyConfused has already pointed out, in the original problem case - deleting a derived class via a pointer-to-base - you still have the same problem. In addition now any time a derived class is properly destructed it calls the base class destructor twice, which is undefined behaviour.

Σnigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!