J
Javier
Hello, is this possible?
I have a pure virtual function in the base class (to force the
programmers of the derived classes to have this function implemented)
but I want to call the derived class function from a base class
reference:
class A
{
public:
virtual void function() = 0;
};
class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}
int main()
{
B derived();
A &base = dynamic_cast<A &>(B); // don't now if this is the way
base.function(); // I need this type of call because I dont't
know the derived class type at runtime.
}
Of course I could have a function2 (private) and make it pure virtual,
and then have function() in base class calling the virtual function2:
class A
{
public:
void function() { this->function_impl() };
protected:
virtual void function_impl() = 0;
};
class B
:
public A
{
protected:
void function_impl() { cout << "Hello" << endl; };
}
int main()
{
B derived();
A &base = dynamic_cast<A &>(B);
A.function();
}
Is this good?
But... can I do it the first way?
Thanks.
I have a pure virtual function in the base class (to force the
programmers of the derived classes to have this function implemented)
but I want to call the derived class function from a base class
reference:
class A
{
public:
virtual void function() = 0;
};
class B
:
public A
{
public:
void function() { cout << "Hello" << endl; };
}
int main()
{
B derived();
A &base = dynamic_cast<A &>(B); // don't now if this is the way
base.function(); // I need this type of call because I dont't
know the derived class type at runtime.
}
Of course I could have a function2 (private) and make it pure virtual,
and then have function() in base class calling the virtual function2:
class A
{
public:
void function() { this->function_impl() };
protected:
virtual void function_impl() = 0;
};
class B
:
public A
{
protected:
void function_impl() { cout << "Hello" << endl; };
}
int main()
{
B derived();
A &base = dynamic_cast<A &>(B);
A.function();
}
Is this good?
But... can I do it the first way?
Thanks.