rb@cc.ic.ac.uk (Robin Becker) (02/15/91)
Someone posted the following simple inheritance example recently
here are the results from TC++ and Zortech 2.1
extern "C"
{
#include <stdio.h>
}
class base
{
public:
virtual void whatami() { printf("I'm a base.\n"); }
base() { printf("In the base constructor "); whatami(); }
~base() { printf("In the base destructor "); whatami(); }
};
class derived : public base
{
public:
/* virtual void whatami() { printf("I'm a derived.\n"); } */
derived() { printf("In the derived constructor "); whatami(); }
~derived() { printf("In the derived destructor "); whatami(); }
};
class mostderived : public derived
{
public:
virtual void whatami() { printf("I'm a mostderived.\n"); }
mostderived() { printf("In the mostderived constructor "); whatami(); }
~mostderived() { printf("In the mostderived destructor "); whatami(); }
};
main()
{
mostderived object;
return 0;
}
should print according to the poster:
In the base constructor I'm a base.
In the derived constructor I'm a derived.
In the mostderived constructor I'm a mostderived.
In the mostderived destructor I'm a mostderived.
In the derived destructor I'm a derived.
In the base destructor I'm a base.
If it doesn't -- call your compiler vendor and complain.
Zortech (compiled with ztc -g test.cpp)
In the base constructor I'm a base.
In the derived constructor I'm a base.
In the mostderived constructor I'm a mostderived.
In the mostderived destructor I'm a mostderived.
In the derived destructor I'm a mostderived.
In the base destructor I'm a mostderived.
TC++ (Academic version only using default settings)
In the base constructor I'm a base.
In the derived constructor I'm a base.
In the mostderived constructor I'm a mostderived.
In the mostderived destructor I'm a mostderived.
In the derived destructor I'm a base.
In the base destructor I'm a base.
What can one say? Who is right? Is symmetry important?steve@taumet.com (Stephen Clamage) (02/16/91)
rb@cc.ic.ac.uk (Robin Becker) writes: >Someone posted the following simple inheritance example recently >here are the results from TC++ and Zortech 2.1 >class derived : public base >{ >public: >/* virtual void whatami() { printf("I'm a derived.\n"); } */ > derived() { printf("In the derived constructor "); whatami(); } > ~derived() { printf("In the derived destructor "); whatami(); } >}; Since whatami is commented-out in class derived, there is no way to print the phrase "I'm a derived". Within the constructor or destructor for any class, virtual function calls can refer only to functions in the current or base classes, never to functions in any derived class. (In a constructor, the derived class doesn't exist yet, so you can't refer to its members. In a destructor, the derived class is gone, so you can't refer to its members.) For the code you show, Borland got it right, Zortech is wrong. -- Steve Clamage, TauMetric Corp, steve@taumet.com
jimad@microsoft.UUCP (Jim ADCOCK) (02/20/91)
In article <604@taumet.com> steve@taumet.com (Stephen Clamage) writes: |rb@cc.ic.ac.uk (Robin Becker) writes: | |>Someone posted the following simple inheritance example recently |>here are the results from TC++ and Zortech 2.1 | |>class derived : public base |>{ |>public: |>/* virtual void whatami() { printf("I'm a derived.\n"); } */ |> derived() { printf("In the derived constructor "); whatami(); } |> ~derived() { printf("In the derived destructor "); whatami(); } |>}; | |Since whatami is commented-out in class derived, there is no way to |print the phrase "I'm a derived". Sorry for the confusion! I intended to post a version where whatami() above was not commented out [I commented out the above line while double- checking my results] With the line commented back in, my original statements should hold. |Within the constructor or destructor for any class, virtual function calls |can refer only to functions in the current or base classes, never to |functions in any derived class. (In a constructor, the derived class |doesn't exist yet, so you can't refer to its members. In a destructor, |the derived class is gone, so you can't refer to its members.) I agree. The appropriate reference is ARM section 12.7 page 294.