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:
warning: will never be executed
qui renvoient tous sur le destructeur de basic_string:
Code : S�lectionner tout - Visualiser dans une fen�tre � part
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 : S�lectionner tout - Visualiser dans une fen�tre � part
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 : S�lectionner tout - Visualiser dans une fen�tre � part
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