You're privately inheriting from std::string. Private inheritance means nobody else knows about the inheritance. This means that nobody will be able to treat your string2 like an std::string, because the std::string portion is private. It's basically just an ugly form of composition. If you think that public inheritance solves your problem, think again. std::string's destructor is not virtual.
Furthermore, in addition to your private inheritance, you also have an std::string data member. This is redundant, as your string2 class already inherited all the members of std::string. If you want to extend the functionality of std::string, you have a few options.
You can either wrap it (no inheritance, store a std::string data member, and tunnel function calls to it):
class string2
{
public:
void push_back(char _ch)
{
mString.push_back(_ch);
}
private:
std::string mString;
};
Or you can wrap it (private inheritance, no std::string data member, and still tunnel function calls to it):
class string2 : private std::string
{
public:
void push_back(char _ch)
{
std::string::push_back(_ch);
}
};
Or you can write non-member utility functions that manipulate it, and bundle them in a namespace:
namespace string2
{
char GetFirstLetter(const std::string& str)
{
// blah blah
}
}
Bonus mention by juan: You can also write this ugliness if you never want to pass a code review:
class string2 : private std::string
{
public:
using std::string::push_back;
};
Note that push_back() is just for example. You'd need to do this for every member of std::string you want exposed.