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

Posted on 2001-07-10
Medium Priority
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... :(
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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 600 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

719 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