struct A {
void f() { }
};
struct B : A {
void f(int) { }
};
int main() {
B obj_B;
obj_B.f(3);
// obj_B.f();
}
コンパイラーは、void B::f(int) の宣言が A::f() を隠しているので、
関数呼び出し obj_B.f() を許可しません。struct A {
void f() { }
};
struct B : A {
using A::f;
void f(int) { }
};
int main() {
B obj_B;
obj_B.f(3);
obj_B.f();
}
クラス B に using 宣言があるので、
名前 f は 2 つの関数で多重定義されます。これで、コンパイラーは、関数呼び出し obj_B.f() を許可します。#include <iostream>
using namespace std;
struct A {
void f() { }
void f(int) { cout << "void A::f(int)" << endl; }
};
struct B : A {
using A::f;
void f(int) { cout << "void B::f(int)" << endl; }
};
int main() {
B obj_B;
obj_B.f(3);
}
上記の例の出力は、以下のとおりです。void B::f(int)
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "void A::f()" << endl; }
virtual void f(int) { cout << "void A::f(int)" << endl; }
};
struct B : A {
using A::f;
void f(int) { cout << "void B::f(int)" << endl; }
};
int main() {
B obj_B;
A* pa = &obj_B;
pa->f(3);
pa->f();
}
この例で、B::f(int) は仮想関数であり、using A::f; 宣言が指定されていても、A::f(int) をオーバーライドします。
この出力は以下のとおりです。
void B::f(int)
void A::f()