F
fabio.bizzetti
Hello all,
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.
Here we go: I noticed that if I nest a structure inside another one,
the wrong constructor will be called.. the one from a different class!!
Here is some test code:
---
struct One {
int o;
One() {
printf("Into the constructor of One which is inherited by ");
};
};
struct Two : One {
int t;
Two() {
printf("Two\n");
};
};
struct Zwei : One {
int z;
Zwei() {
printf("Zwei\n");
};
};
struct Dos : One {
int d;
Dos() {
printf("Dos\n");
};
};
struct MyFinalStruct_this_acts_bug_free {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;
struct MyFinalStruct_this_produces_the_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;
int main() {
printf("...testing the first structure:\n");
MyFinalStruct_this_acts_bug_free Test1;
printf("...testing the second structure:\n");
MyFinalStruct_this_produces_the_bug Test2;
// --
return(0);
}
---
The above program produces this output, at least on my compiler:
---
....testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
....testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
---
Is this normal? The first output is what I would definitely expect
also from the other structure!
Thanks,
Fabio
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.
Here we go: I noticed that if I nest a structure inside another one,
the wrong constructor will be called.. the one from a different class!!
Here is some test code:
---
struct One {
int o;
One() {
printf("Into the constructor of One which is inherited by ");
};
};
struct Two : One {
int t;
Two() {
printf("Two\n");
};
};
struct Zwei : One {
int z;
Zwei() {
printf("Zwei\n");
};
};
struct Dos : One {
int d;
Dos() {
printf("Dos\n");
};
};
struct MyFinalStruct_this_acts_bug_free {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;
struct MyFinalStruct_this_produces_the_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;
int main() {
printf("...testing the first structure:\n");
MyFinalStruct_this_acts_bug_free Test1;
printf("...testing the second structure:\n");
MyFinalStruct_this_produces_the_bug Test2;
// --
return(0);
}
---
The above program produces this output, at least on my compiler:
---
....testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
....testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
---
Is this normal? The first output is what I would definitely expect
also from the other structure!
Thanks,
Fabio