andrew@hrc63.co.uk (Andrew Haylett "Baddow") (05/09/89)
A mysterious problem with C++: your help would be appreciated. The problem seems to be that when an instance of a derived class is created and the only matching constructor is in the base class, this constructor is used. Fair enough, but this leaves the virtual function pointer referring to the base class virtual function. Is this: (a) a language feature (b) a compiler feature (we use Oregon C++, version 1.2a) ----- cut here ---------------------------------------- #include <stream.h> class A { public: A() { }; A(int z) { }; virtual void f() { cout << "a\n"; }; }; class B : public A { public: B() { }; // B(int z) { }; // This constructor is missing void f() { cout << "b\n"; }; }; main() { A a (1); // instance of A, one argument B b1 ; // instance of B, calls B's constructor B b2(1); // instance of B, calls A's constructor a.f(); // says "a" b1.f(); // says "b" b2.f(); // says "a" -- WRONG -- should be "b" } Andrew Haylett, GEC Research
mball@cod.NOSC.MIL (Michael S. Ball) (05/11/89)
In article <583@hrc63.co.uk> andrew@hrc63.co.uk (Andrew Haylett "Baddow") writes: >A mysterious problem with C++: your help would be appreciated. > >The problem seems to be that when an instance of a derived class is >created and the only matching constructor is in the base class, this >constructor is used. Fair enough, but this leaves the virtual function >pointer referring to the base class virtual function. Is this: It's a bug, you need an update to the compiler. I suggest you call and get the latest version from Oregon Software. Actually, it was an attempt to imitate a similar bug in cfront so some existing code would work. We eventually decided it was just a bug and the user should fix his code. Sorry you got caught. Mike Ball Taumetric Corporation 1094 Cudahy Pl. Ste 302 San Diego, CA 92110 (619)275-6381 mball@cod.nosc.mil
ark@alice.UUCP (Andrew Koenig) (05/11/89)
In article <583@hrc63.co.uk>, andrew@hrc63.co.uk (Andrew Haylett "Baddow") writes: The problem you describe appears to be an unplanned feature in your compiler. class A { public: A() { }; A(int z) { }; virtual void f() { cout << "a\n"; }; }; class B : public A { public: B() { }; // B(int z) { }; // This constructor is missing void f() { cout << "b\n"; }; }; main() { A a (1); // instance of A, one argument B b1 ; // instance of B, calls B's constructor B b2(1); // instance of B, calls A's constructor Bug #1 -- B doesn't have a B::B(int) constructor, so the compiler should reject this call. Constructors are not inherited. a.f(); // says "a" b1.f(); // says "b" b2.f(); // says "a" -- WRONG -- should be "b" Bug #2. b2 is a B, not an A, and should say so. } -- --Andrew Koenig ark@europa.att.com