class A {
public:
void fa() {
}
};
class B : public A{
public:
void fb() {
}
};
class C : public A, public B {
public:
void fc() {
//call A::fa(), not B::A::fa();
}
};
How to call A::fa()
from C::fc()
function.
GCC warns withdirect base A inaccessible in C due to ambiguity
, does this mean there is no direct way to refer base class members?
A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method.
No. A reference to a derived class must actually refer to an instance of the derived class (or null).
If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private . If the derived class is declared with the keyword struct , the default access specifier in its base list specifiers is public .
One option would be to create a stub class that you can use for casting to the right base class subobject:
struct A {
void fa() { }
};
struct B : A {
void fb() { }
};
// Use a stub class that we can cast through:
struct A_ : A { };
struct C : A_, B {
void fc() {
implicit_cast<A_&>(*this).fa();
}
};
Where implicit_cast
is defined as:
template <typename T> struct identity { typedef T type; }
template <typename T>
T implicit_cast(typename identity<T>::type& x) { return x; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With