How do I inherit from STL string, and keep operators...

Posted on 2001-07-10
Last Modified: 2013-12-14
I have tried to create a new class, that inherits from std::string, because I want to add additional functionality to the string class.

I have done like this :

class CMyString : public std::string

But I loose some of the operators (I don't know if I loose them all) like the assign operator that makes
  myString = "stringdata";
possible. It seems like i loose the operators from std::basic_string, that std::string inherit from.

The question is, can I somehow inhereit from std::string, without loosing f.ex the assign operator ?
I wouldn't like, to override all the needed operators, but still want to be able to use the CMyString class as if it was an std::string.
Question by:kalle12
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2

Expert Comment

ID: 6268548
> But I loose some of the operators

The problem is that you do not have appropriate constructors in your class, constructors are not inherited in the same way as other members of the class, and by default only the default constructor of string will be called.

If you have no other constructors in your class, add one like this:

    template <typename T>
    CMyString(T value)
        : std::string(value)

this should allow your class to work.
LVL 22

Expert Comment

ID: 6268570
>> inherits from std::string, because I want to add additional
>> functionality to the string class.
string was never meant to be inherited from.  Inheriting from it is very questionable.  its likey to be problematic.  In fact inheritance really was not designed into the STL except for the stream classes.  There are almost always other ways to provide what you need.  What is it that you want to do?

>>  I loose some of the operators (I don't know if I loose them all) like
>> the assign operator that makes
Operator = is never inherited.  You need to provide your own.  (The default operator =, the one that assigns from an object of your class type will be provided automatically if possible, but the other oeprator =()'s, ones that assign from other data types will not be inherited.)

Author Comment

ID: 6268949
Well - the first comment (jasonclarke) didn't help me - or I didn't know exactly what to do - sorry.

nietod> What I want to do, is to add functions to std::string (for a start) to make serialization possible easily. ( not only serialization of strings, but also of string lists (std::list< CStdString > ) and other objects.

So I have made the functions pSerializeIn / pSerializeOut on my CStdString class, and on my listclass.

I might need just to create a collection of functions, that can take a list / stringlist as argument, and then do the serialization. I just liked the idea about inheriting from std::list a lot better (which would also make i t possible to let all my types inherit from a generel CSerializeBase class... :(
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6268971
Here is an expanded example, using the approach I described earlier:

#include <string>

using namespace std;

class mystring : public string
    mystring() {}

    mystring(const char* value)
        : string(value)

int main()
    mystring ms;

    ms = "abcdefg";

    return 0;

this works and relies on the default assignment operator for your string class.
LVL 22

Accepted Solution

nietod earned 200 total points
ID: 6269016
>> the first comment (jasonclarke) didn't help me - or I
>> didn't know exactly what to do - sorry.
it solves one problem you will face.  However, jason solved it with a function femplate.  This is a pretty fancy way to solve the problem.  Its veyr easy and powerful.  Unfortuantely, many compilers can't support it it.  (They should, it is completely legitimate C++, but it pushes the capabilities of many current compilers.)

As an alternative, you coudl look at the constructors of the string class and then provide coresponding constructors in your class, like

class CMyString : public std::string
   CMyString(const char *s) : string(s) {};
   CMyString(const char *s,size_type n) : string(s,n) {};
   CMyString(const CMyString &s) : string(s) {};
   CMyString(const string &s,size_type pos,size_type n) : string(s,pos,n) {};

   // and so on.  There are bunch more constructors.


>> What I want to do, is to add functions to std::string (for a start) to
>> make serialization possible easily
Probably the best way to do this is to use operator << and operator >> for serialization.  These are written as non-member functions so you can use them with classes that you have not written--like string.

Why is a non-member function like operator << better?  than a member function.  In addition to not forcing you to alter the interface of existing classes to fit your scheme, it also allows you to this scheme with non-clas types, like int, char etc.  You may be thinking, well "int" is not an object, I don't want to serialize it, but that is wrong".  consider how you will approach serialzing a list<>.  it will do this by serializing each item in the list.  If it can do this with operator << then it can serialize a list of anything.  i.e. it can serialie a list of obkjects, a list of lists of objects, or just a plain old list of ints.  See how this one approach allows you to work with all data types (classes and PODs) in the same manner, which makes the work much easier.

This is why the STL stream classes use << an >>
LVL 22

Expert Comment

ID: 6269032
>> this works and relies on the default assignment operator for your string class.
That does work.  What is happening there is that the source value, the character string  is being converted to an unammed temporary CMyString object by the compiler using the CMyString constructor that jason wrote.  Then that unnamed temporary object is assigned to the destination class using the default operator.

The actual string class would not do this this way.  it has an overloaded operator = that takes a character array as a source value.  So it does not require the creation of the unnamed temporary, wnich could be more efficient.   but this operator = is not inheritred.  If you wanted to use it, you would have to add the cooresponding operaotr ==to the CMyString class.

Author Comment

ID: 6269814
I think that something like the << and >> will be the solution (which is actually what I am planning on doing with my serializeIn / serializeOut functions if inheritance is not possible). I did try the constructors, but I'm not happy with all the work, that I will have to do, to keep the original interface for std::string (I would also need to write other operators to support the std::string inteface in my class ).

But It was a really fine way, to solve the assign problem.

Thanks a lot for your answers...

/ k
LVL 22

Expert Comment

ID: 6269845
>> (I would also need to write other operators to support the std::string inteface in my class ).
The constructors and the assignment operators woudl be the bulk of it.  But you probalby wouldn't like the result.  string wasn't meant to be inherited from and you would probably run into awkward problems.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 125
oracle 11g 23 151
I could not set window to top 4 63
How to add columns to a RAD Studio FMX grid at design time 1 61
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question