スーパークラスの関数のオーバーロードを防ぐ何かがC++標準にありますか?スーパークラスの関数のオーバーロード
クラスのこのペアを皮切り:私は簡単にB::foo()
を呼び出すことができます
class A { // super class
int x;
public:
void foo (int y) {x = y;} // original definition
};
class B : public A { // derived class
int x2;
public:
void foo (int y, int z) {x2 = y + z;} // overloaded
};
:
B b;
b.foo (1, 2); // [1]
をしかし、私はA::foo()
を呼び出そうと...
B b;
b.foo (12); // [2]
..コンパイラエラーが発生する:
過負荷がないように210test.cpp: In function 'void bar()':
test.cpp:18: error: no matching function for call to 'B::foo(int)'
test.cpp:12: note: candidates are: void B::foo(int, int)
はちょうど私が何かが欠けていなかったことを確認するために、私はB
の関数の名前を変更:
class B : public A {
int x2;
public:
void stuff (int y, int z) {x2 = y + z;} // unique name
};
そして今、私は2番目の例を使用してA::foo()
を呼び出すことができます。
この規格はありますか?私はg ++を使用しています。
は元のメンバ関数を "非表示にする"。 –
+1 - Scott MeyersのEffective C++でこれがカバーされていることに注意してください。Item 33:継承された名前を隠すことは避けてください。 –