Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Virtual Functions Calls Inside Constructors
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Marcelo De Brito, post: 3858302"] Hi! I have read that only the local version (overriden) of a virtual function is available to be called inside a constructor of a class that inherits from another (base) class (which owns the "base" virtual function overriden by the derived one). But the code I have written compiles fairly. See it: #include <iostream> using namespace std; class b1 { public: b1() { cout << "b1::b1()" << endl; } virtual void f() { cout << "b1::f()" << endl; } }; class d1 : public b1 { public: d1() : b1() { cout << "d1::d1()" << endl; b1::f(); //Calls the base class "f()" function. Shouldn't it generate an error? f(); //Calls the local class "d1::f()" function } void f() {cout << "d1::f()" << endl;} }; int main() { d1 objd1; } I got the following return: b1::b1() d1::d1() b1::f() d1::f() I thought the base class "b1::f()" function would be unavailable for calling inside the derived class' constructor and only the local version "d1::f()" could be called. I appreciate your comments, suggestions, etc. Thank You! Marcelo [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Virtual Functions Calls Inside Constructors
Top