gened@gssc.UUCP (Gene Daniel) (09/06/90)
I think that I have found a bug in Turbo C++. If this has been discussed
here before feel free to ignore this article. The following program calls
a member function in the base class which in turn calls a virtual member
function. With Turbo the virtual function in the base class is called
if the instance is a Base class or a Derived class. With Zortech the virtual
function in the derived class is called if the instance is a Derived class
and the function in the base class is called if the instance is a Base class,
which is what I would expect. One interesting item is that if the base class
is declared as virtual then things seem to work as they as they do in Zortech.
Is this really a bug?? Which compiler got it right???
---------------------------- Cut Here ------------------------------------
#ifdef __ZTC__
#include <stream.hpp>
#else
#include <iostream.h>
#endif
class Base {
int i;
public:
Base() {i = 0; }
virtual void MemberFunc(char *instanceName) {
cout << instanceName << ": virtual MemberFunc in Base\n";
}
void CallMemberFunc(char *instanceName) { MemberFunc(instanceName); }
};
class Derived: public Base { // change to virtual public Base and it works!!!!
public:
virtual void MemberFunc(char *instanceName) {
cout << instanceName << ": virtual MemberFunc in Derived\n";
}
};
main(int argc, char *argv[])
{
Derived derived;
Base base;
base.CallMemberFunc("base");
derived.CallMemberFunc("derived");
}
---------------------------------- Cut Here ----------------------------------
The output from Zortech C++ is:
base: virtual MemberFunc in Base
derived: virtual MemberFunc in Derived
The output from Turbo C++ is:
base: virtual MemberFunc in Base
derived: virtual MemberFunc in Base
--
Gene Daniel | gened@gss.com
Graphic Software Systems, Inc. | or
P.O. Box 4900 | ..!tektronix!sequent!gssc!gened
Beaverton, Oregon 97005-7161 |beard@ux5.lbl.gov (Patrick C Beard) (09/16/90)
In article <6322@gssc.UUCP> gened@gssc.UUCP (Gene Daniel) writes:
#
#I think that I have found a bug in Turbo C++.
#The following program calls
#a member function in the base class which in turn calls a virtual member
#function. With Turbo the virtual function in the base class is called
#if the instance is a Base class or a Derived class. With Zortech the virtual
#function in the derived class is called if the instance is a Derived class
#and the function in the base class is called if the instance is a Base class,
#which is what I would expect. One interesting item is that if the base class
#is declared as virtual then things seem to work as they as they do in Zortech.
#
#Is this really a bug?? Which compiler got it right???
I believe it is a bug. I've tried your program on a Mac IIfx with MPW C++
3.1b4 and it behaves as you describe Zortech does.
--
-------------------------------------------------------------------------------
- Patrick Beard, Macintosh Programmer (beard@lbl.gov) -
- Berkeley Systems, Inc. ".......<dead air>.......Good day!" - Paul Harvey -
-------------------------------------------------------------------------------