Re: Accessing virtuals in base class
Jo wrote:
:: Zeppe wrote:
::
::: Jo wrote:
:::
:::: Juha Nieminen wrote:
::::
::::: Jo wrote:
:::::
:::::
:::::: I would like to do this in a generic way, thus without having
:::::: to put this line in every class specifying the proper base
:::::: class
::::::
:::::
:::::
::::: How could the compiler know *which* version of the function
::::: you want to call if you don't specify it?
:::::
::::: Just call: Base::Foo(something);
::::
::::
:::: I mean the immediate base class "one up" in the inheritance tree.
::::
:::: That should be no problem for the compiler.
:::
:::
::: no? what about this:
:::
::: class A
::: {
::: public:
::: virtual ~A() {}
::: virtual void foo() { std::cout << "A::foo\n"; }
::: };
:::
::: class B
::: {
::: public:
::: virtual ~B() {}
::: virtual void foo() { std::cout << "B::foo\n"; }
::: };
:::
::: class C
::: : public A, public B
::: {
::: public:
::: virtual ~C() {}
::: virtual void foo() { std::cout << "C::foo\n"; }
::: };
:::
::: what function should call C, given that inherits from two
::: different classes that define the same virtual function? On the
::: other side, I don't see what kind of problem do you have in
::: specifying the base class, when it's needed.
:::
::
:: Agreed, in this case the compiler could warn that there is
:: ambiguity of course. Just like the compiler does warn about
:: ambiguity in some other cases.
::
:: But in many cases the corresponding direct base class function
:: should be very definable.
If you need to refer to the direct base class, you can add a typedef
to you class.
class C : public A, public B
{
typedef A direct_base_class; // select the one you want
public:
virtual void foo()
{
direct_base_class::foo();
// do someting more
}
};
::
:: But reading your answers i suppose there is no such syntax to do
:: so..?
No, but you can easily use existing features when needed.
Bo Persson