jmasterx
jmasterx

Reputation: 54123

Inheriting and overriding functions of a std::string?

Since std::string is actually a typedef of a templated class, how can I override it? I want to make a UTF-8 std::string that will return the correct length, among other things.

Upvotes: 12

Views: 6390

Answers (8)

Daniel Lidström
Daniel Lidström

Reputation: 10270

It is generally considered a mistake in C++ to derive from a standard library container. However, the functionality you are looking for has already been implemented. Have a look at Glib::ustring.

Hope this helps!

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363627

If you must define your own string type, then don't inherit from std::string but define your own Character Traits class and do something like

typedef std::basic_string<unsigned char, utf8_traits> utf8string;

See also Herb Sutter's website.

Upvotes: 19

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133024

DON'T DERIVE FROM STRING

std::string, that is, basically the whole basic_string template is not designed to be derived from. There are zillions of articles about that already. It doesn't have any virtual functions so there is nothing to override. The best you can do is hide something. Best is to use composition/aggregation! That is, just keep a member of type string in your class and forward the calls! Again, just to make sure

DON'T DERIVE FROM STRING

Upvotes: 16

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

Just be sure you know what you are doing first. What is exactly the "correct length" you want to return from your string objects? Number of code points? That does not always correspond to the number of characters as perceived by the user.

Anyway, take a look at the utf8-cpp library to see an alternative approach to deriving from std::string.

Upvotes: 1

tyree731
tyree731

Reputation: 483

Writing a unicode implementation that conforms and works properly in every circumstance is very difficult to do. I would advise you to use an existing library or implementation instead of rolling your own. For example, Windows, OSX and Qt all have libraries which support UTF-16 and other encoded strings.

Upvotes: 0

icecrime
icecrime

Reputation: 76765

As is has already been stated by others : don't derive from std::string, it's just not designed for this.

You should have a look on this article, which shows how to create a case insensitive string class as an example. You will see that the logic implemented in std::basic_string is independent of the character type, and that providing some custom char_traits should do the trick.

Upvotes: -1

user500944
user500944

Reputation:

Better idea: create an STL-compatible utf8_string container without inheriting from std::string.

Upvotes: 0

bmargulies
bmargulies

Reputation: 100051

  1. Have you looked at ICU?

  2. A typedef is just a convenient label.

    class foo : public bar {} ;

works just fine when bar is a typedef of a PT.

It may not be a good idea in this case, but the language supports it.

Upvotes: 1

Related Questions