5
ここの例は意味をなさないが、これは基本的に私のプログラムをPythonで書いたもので、今はC++で書き直している。私はまだC++で複数の継承を把握しようとしています。ここで行う必要があるのは、メインからCのインスタンスを経由してA :: a_printにアクセスすることです。次に、私が何を話しているかを見ていきましょう。これは可能ですか?仮想派生クラスのメンバー/メソッドへのアクセス
#include <iostream>
using namespace std;
class A {
public:
void a_print(const char *str) { cout << str << endl; }
};
class B: virtual A {
public:
void b_print() { a_print("B"); }
};
class C: virtual A, public B {
public:
void c_print() { a_print("C"); }
};
int main() {
C c;
c.a_print("A"); // Doesn't work
c.b_print();
c.c_print();
}
ここではコンパイルエラーです。
test.cpp: In function ‘int main()’:
test.cpp:6: error: ‘void A::a_print(const char*)’ is inaccessible
test.cpp:21: error: within this context
test.cpp:21: error: ‘A’ is not an accessible base of ‘C’
ビンゴ。それはそれを得た。ありがとう。 – Scott
これは良い点です。両方の継承パスの一方を公開アクセスにすることで十分です。取得されるパスは、最もアクセスを提供するパスです。 –