Solved

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

Posted on 2001-07-10
8
387 Views
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.
0
Comment
Question by:kalle12
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Expert Comment

by:jasonclarke
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.
0
 
LVL 22

Expert Comment

by:nietod
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.)
0
 

Author Comment

by:kalle12
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... :(
0
 
LVL 9

Expert Comment

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

#include <string>

using namespace std;

class mystring : public string
{
public:
    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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 22

Accepted Solution

by:
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 >>
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 

Author Comment

by:kalle12
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
0
 
LVL 22

Expert Comment

by:nietod
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now