Archived

This topic is now archived and is closed to further replies.

Kylotan

Deriving from std::string

Recommended Posts

Ok, I have been using my own KString class (the K meaning Kylotan... egotistical, hmm? ) in several of my projects, and the standard library''s std::string in others. Basically I figured it would make sense to use the same function names across all my projects and so on, but to extend the class in the project that needs it most. And although I am quite satisfied that my KString class is stable and bugfree, I would somehow feel safer using library code So I was intending simply doing this: class MyString : public std::string {}; (With appropriate extra functions of course, not just an empty body.) And before I make the somewhat significant step of converting my whole project, I wondered if anyone knew any pros or cons of this approach? How is the compilation speed affected, going from a non-template class to a templated one? Does it even count as a templated class any more, given that every instance of it will be using the standard allocator and ''char'' type? Will this have a significant effect on my executable size? (Indeed, does std::string increase executable size? Never used it in a large project before.) Any experiences or opinions welcomed.

Share this post


Link to post
Share on other sites
As stated in "The C++ Programming Language" string was intended to be a concrete type. In other words, it was not designed to have anything derived from it. None of its functions are virtual, which effectively means that if you overload any of string''s functions (including the destructor), bad things will happen. So, proceed with caution.

Otherwise, it''s fine. Although it make take longer to compile this way, given the sheer number of functions in the class and it''s template nature, the linker should know not to include unused stuff in your executable, so it shouldn''t be significantly bigger--if at all.

Share this post


Link to post
Share on other sites
(Note to moderator: how did this post end up in this forum? I never even read the Graphics forum, never mind post in it, so I can only assume there was some sort of bug in the posting. Sorry to any graphics people reading this who don''t want clutter in their forum )

Share this post


Link to post
Share on other sites
Use composition;

class String
{
private:
string s;
whateverelse wee;
};

Then add your additional functions and create the ones to wrap the original string methods.

ECKILLER

Share this post


Link to post
Share on other sites