Constructeur de copie (ou autre problème)
Salut.
Me revoilà... encore...
Dans ma super class ebovar, j'essaye surcharger l'opérateur << afin de retourner la concaténation de 2 chaines
Je rappelle à quoi ressemble ma class (qui est sensé représenter un variant).
Pour les chaîne j'utilise un pointer de string.
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 54
| class ebovar
{
public:
int var_type;
union {
int i;
float f;
bool b;
string *s;
map<ebovar, ebovar> *m;
void *o;
};
// var is null
ebovar() {
i =0;
var_type = EBO_NULL;
}
~ebovar()
{
if (var_type == EBO_STRING)
{
printf("-- delete string %s\n", s->c_str());
delete s;
var_type = EBO_NULL;
}
else
printf("-- delete ebovar %s %d\n", this->getType().c_str(), i);
}
ebovar(const ebovar &source)
{
printf("++++ create copy of ebovar\n");
}
// ...
// ...
// ...
ebovar(const char *data) {
s = new string(data);
printf("++ create string %s\n", s->c_str());
var_type = EBO_STRING;
}
ebovar(string data) {
s = new string(data);
printf("++ create string from ebovar %s\n", s->c_str());
var_type = EBO_STRING;
}
// ...
// ...
// ...
} |
Et ma surcharge est la suivante
Code:
1 2 3 4
| ebovar operator<<(const ebovar &left_op, const ebovar &right_op)
{
return ebovar(*(left_op.s) + *(right_op.s));
} |
Tout marche sauf dans 1 cas, celui ou la destination est une des source de l'operation
Code:
1 2 3 4 5 6 7 8 9 10 11
| // Ce code fonctionne
ebovar _s1 = "Hello ";
ebovar _s2 = "World !";
ebovar _s3 = _s1 << _s2;
echo (_s3);
// Ce code ne fonctionne pas
ebovar _s1 = "Hello ";
ebovar _s2 = "World !";
_s1 = _s1 << _s2;
echo (_s1); |
Le deuxième code génère l'erreur suivante
Code:
1 2 3 4 5
| ++ create string Hello
++ create string World !
++ create string from ebovar Hello World !
-- delete string Hello World !
Segmentation fault |
C'est comme si le problème venait du = qui n'arrive pas a transférer un object ebovar vers un autre déjà existant...
j'ai cru que le problème venait du fameux constructeur de copie que je n'avais pas fait, mais il ne semble pas être appelé dans ce cas
Faut-il surcharger le = ?