problème avec std::string
Bonjours,
Voila mon petit problème:
je suis en train de ma faire une petite classe String qui sert juste à rajouter des fonctionnalités à la classe std::string pour que ça soit plus pratique à utiliser.
Donc au début je me suis dit que le plus simple pour pas avoir à retaper les méthodes déjà existantes avec std::string serait que ma classe String hérite de std::string.
Hors, après compilation, j'ai toute un flopée de warnings:
Citation:
warning: will never be executed
qui renvoient tous sur le destructeur de basic_string:
Code:
1 2
| ~basic_string()
{ _M_rep()->_M_dispose(this->get_allocator()); } |
Donc je me suis dit qu'il n'était peut-être pas bon d'hériter un std::string.
Est-ce que cela est un fait connu ou y a pas de problèmes à priori? (déjà le destructeur est pas virtuel, c'est peut-être pas bon signe).
Enfin, en attendant, je me suis dit que j'allais mettre ma string en membre de ma classe String plutôt que d'en hériter, mais les warnings sont toujours la...
Quelqu'un a une idée de ce qui pourrait se passer?
voici le code de la version encapsulée:
string.hpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #ifndef __STRING_HPP__
#define __STRING_HPP__
#include <string>
#include <iostream>
class String
{
public:
String();
String(char* chain);
String(const char* chain);
//convenience operators
String& operator<<(double d);
String& operator<<(const String& str);
String& operator+(double d);
String& operator+(char* chain);
String& operator+(const String& str);
friend std::ostream& operator<<(std::ostream& o, const String& str);
protected:
std::string innerString;
};
#endif // __STRING_HPP__ |
string.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <core/String>
#include <sstream>
using namespace std;
String::String()
{}
String::String(char* chain): innerString(chain)
{}
String::String(const char* chain): innerString(chain)
{}
String& String::operator<<(double d)
{
std::ostringstream oss;
oss << d;
innerString.append(oss.str());
return *this;
}
String& String::operator<<(const String& str)
{
innerString.append(str.innerString);
return *this;
}
String& String::operator+(double d)
{
std::ostringstream oss;
oss << d;
innerString.append(oss.str());
return *this;
}
String& String::operator+(char* chaine)
{
innerString.append(chaine);
return *this;
}
String& String::operator+(const String& str)
{
innerString.append(str.innerString);
return *this;
}
ostream& operator<<(ostream& o, const String& str)
{
o << str.innerString;
return o;
} |
PS: je développe sous CodeBlocks avec MinGW