Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!uunet!mcvax!ukc!stl!stc!root44!hrc63!andrew From: andrew@hrc63.co.uk (Andrew Haylett "Baddow") Newsgroups: comp.lang.c++ Subject: Virtual functions and missing constructors Message-ID: <583@hrc63.co.uk> Date: 9 May 89 11:36:49 GMT Organization: GEC Hirst Research Centre, Wembley, England. Lines: 40 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 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