M
Marcelo De Brito
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
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