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
  • 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... :(
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.


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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 52
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 67
oracle 11g 23 107
Best environment for cross platform web development? 5 55
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 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