1

次のコードを検討してください。

struct A
{
    void foo( const char * ) { cout << __PRETTY_FUNCTION__ << endl; }
    A & operator = ( const A &  ) { cout << __PRETTY_FUNCTION__ << endl; return * this; }
};


struct B : public A
{
    void foo( const char * ) { cout << __PRETTY_FUNCTION__ << endl; }
    A & operator = ( const A & other ) { cout << __PRETTY_FUNCTION__ << endl; return * this; }
};

次に、このメンバーを呼び出すと、次のようになります。

B b;

b.foo( "hehe" );
b = b;

印刷されます:

void B::foo( const char *)
A& A::operator=(const A&)

質問: なぜ B::foo は A::foo を隠しますが、B::operator= は隠しませんか?

4

1 に答える 1

8

What you see is not a hiding problem at all. You did not create an assignment operator to assign B into B, therefore the compiler created one for you. The one created by the compiler is calling assignment operator of A.

Therefore, if your question "Question: why B::foo hides A::foo, but B::operator= doesn't?" should be read "How is operator= different from an ordinary function", the difference is compiler will provide one for you if you do not write your own.

于 2010-12-06T13:36:22.397 に答える