スーパークラスの関数をオーバーロードすることを防ぐ C++ 標準の何かがありますか?
このクラスのペアから始めます。
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::foo()
:
B b;
b.foo (1, 2); // [1]
でも電話しようとしたらA::foo()
…
B b;
b.foo (12); // [2]
... コンパイラ エラーが発生します。
test.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
};
A::foo()
これで、2 番目の例を使用して呼び出すことができます。
これは標準ですか?g++ を使用しています。