Allan W wrote:[color=blue]
> kanze wrote:[color=green]
>>
olanglois@sympatico.ca wrote:[/color]
> [snip][color=green][color=darkred]
>>> 4- Added the assignment opertor=(const Base &) (otherwise a
>>> temporary Derived object is created...)[/color]
>> You also need to make the destructor of Base virtual. I'd
>> also add a private operator new[] to Derived -- in no case
>> can you allow the construction of C style arrays of Derived
>> on the heap (since they will fatally end up assigned to a
>> Base*).[/color][/color]
[color=blue]
> Yeah, but...[/color]
[color=blue]
> if sizeof(Base)==sizeof(Derived), and if ~Derived() has an
> empty body (i.e. it does nothing but call ~Base()), then there
> isn't likely to be a problem, not even with new Derived[] --
> is there? Yes, I am aware that it violates the standard...
> but show me a program that does this, and a compiler that
> makes it crash?[/color]
In the absense of further derivation, I think in fact it is
safe. I even once toyed with the idea making a proposal to the
standard to guarantee that it work -- basically, defining
"trivial derivation" as adding new constructors and nothing
else, and then saying that it would work in the case of trivial
derivation. In the end, it seemed too much bother, and too much
special case. And not really that useful.
[color=blue]
> #include <iostream>
> #include <ostream>[/color]
[color=blue]
> struct Base {
> int x;
> // whatever...
> Base(int z=0) : x(z) {}
> ~Base() { std::cout << "Destroy: " << x << '\n'; }
> };[/color]
[color=blue]
> struct Derived : public Base { // No Multiple Inheritance
> static int nextX;
> Derived() : Base(nextX++) {}
> // No new non-static data members
> //~Derived(); -- Does not explicitly define the destructor
> };
> int Derived::nextX = 100;[/color]
[color=blue]
> int main() {
> {
> Base a(10);
> Base *b = new Derived[5];[/color]
[color=blue]
> delete[] b; // Deleting with wrong data type, violates
> standard...
> // but is it EVER a problem in these limited situations?
> // Calls ~Base() directly, instead of going through
> ~Derived()...
> // But all ~Derived() does is forward to ~Base(), right?
> }[/color]
[color=blue]
> std::cout << "Fin" << std::endl;
> }[/color]
[color=blue]
> Even Comeau doesn't complain about this code
> (haven't tried running it with Comeau, though)
> I did try it with Microsoft, it gave the obvious results.[/color]
Well, it's undefined behavior, and I doubt you'll find a
compiler which complains. But because it's undefined behavior,
the fact that a compiler gives the obvious results once doesn't
prove anything. (In theory, at least. In practice, knowing how
compilers work, I can't imagine an implementation where it would
fail.)
--
James Kanze
kanze.james@neuf.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]