5

I just miss some functions in the c++ standard library string class, so I just wanted to add these by myself. I wrote this:

#include <string>


class String : public std::string
{
public:
    // some new fancy functions
};

and later noticed by reading some forums, that it is a bad idea to inherit from std::string and any other container from the standard library.

I just want the normal string, but with additional functions written by myself, how can I achieve this in the right way? Or is there no way to do it right and I have to right my own string class?

1
  • 2
    This is the most right way of doing it, and it is usually a bad idea. If you want new functionalities, you are better off writing functions that work with std::string instead. Bonus points if they are generic and work with other containers too. Commented Mar 5, 2020 at 20:28

1 Answer 1

9

tl;dr: Use freestanding functions.

First of all - std::string is kind of a mess, and has too many methods as-is. It's bad design to lump functionality into a class which doesn't need to be in that class, and can easily be implemented using simpler, more basic class methods - as a freestanding function.

Moreover - std::string is at the same time unwieldy to manipulate (it's not a string buffer or an std::stringstream), and not impossible to manipulate, i.e. not immutable.

But coming back to my earlier point: The "right way" - if there is any - to do what you wanted is with freestanding functions. For example, suppose you want to randomly permute the contents of an std::string. Well, either:

std::string& jumble(std::string& str)

or

std::string jumble(std::string str)

or maybe, if you want to feel cool and micro-optimized,

std::string jumble(const std::string& str)
std::string jumble(std::string&& str)

depending if you want to use strings more as immutable or as mutable entities.

Also remember, that we don't really have a single std::string class - we have a template based on the character type (and an allocator etc.), so if you want to be generic, you have to accept this class:

template<
    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>
> class basic_string;

PS - If we had uniform call syntax, like Bjarne proposed - which we really should IMHO - your freestanding functions could simply be invoked as though they were members:

auto jumbled = my_string.jumble();
Sign up to request clarification or add additional context in comments.

7 Comments

Oh, please not uniform call syntax. One of the strengths of C/C++ is, that they call the different things by their proper names, and don't try to hide complexity behind automagic facades. Well, C much more so than C++, actually. Uniform call syntax would just be another step to turn the already hopelessly overfeatured C++ into something like javascript, which may look slick at the first glance, but actually makes it pretty hard to deduce what a statement actually does. Of course, C++ has always moved into that direction, auto being the worst offender, but every further step is harmful imho.
"Uniform call syntax" would actually make things more explicit and C-like. It would reflect the inherent reality of a (non-static) method being just a function which takes a "this" pointer, can access private members, and has some syntactic sugar to avoid saying "this->" all the time. It would be very clear that if you write x.bar(y) you really mean bar(&x, y) - just as a->z is the same as saying (*a).z.
@cmaster-reinstatemonica, also with the help of IntelliSense of modern editors/IDEs whenever you put your cursor on auto, it will say you what type exactly it is, but in javascript you can't realize which type has a variable! as you know auto is a compile-time feature so it's easy to detect what it is for any editor and IDEs. and it makes life to get easier for us.
@SdSaati Which means that a) I must use some IDE to even read the code, and b) I must use my mouse. Both things that I'd rather avoid or might not even have. Like when viewing source code online, or when being logged into some machine via ssh. An explicitly written type a) works independent of tooling, b) reflects programmer intention rather than compiler understanding, and c) produces an error when the two don't match up. I realize that this is hard to understand for someone who's not been socialized with explicit types, but for old hands like me, types carry a lot of meaning.
@cmaster-reinstatemonica even vim can detect and show that, it also works inside ssh, tmux and every other environment, being explicit is Ok, but I just wanted to mention that javascript var is not the same as auto in C++
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.