C ++: virtual assignment operator

why, if we have a pure virtual assignment operator in the base class, then we implement this operator in the derived class, it gives a linker error in the base class?

Currently, I only have the following explanation http://support.microsoft.com/kb/130486 , he said that the behavior is by design, since normal inheritance rules do not apply .

I don’t understand why it generates a design linker error? can someone give me a clearer explanation on this?

edit: added my simplified code from which the error occurred:

class __declspec(dllexport) BaseClass {
public:
    int memberA;
    virtual BaseClass& operator=(const BaseClass& rhs) = 0;
};

class __declspec(dllexport) DerivedClass : public BaseClass {
public:
    int memberB;
    DerivedClass():memberB(0) {}
    virtual BaseClass& operator=(const BaseClass& rhs) {
        this->memberA = rhs.memberA;
        this->memberB = 1;
        return *this;
    }
};

int main(void)
{
    DerivedClass d1;
    DerivedClass d2;

    BaseClass* bd1 = &d1;
    BaseClass* bd2 = &d2;

    *bd1 = *bd2;
}

__declspec(dllexport) / = .

__declspec(dllexport) *bd1 = *bd2;, d1:: memberB 1, __declspec(dllexport) d1:: memberB

__declspec(dllexport) *bd1 = *bd2;, d1:: memberB

+5
3

12.8 :

13 X . X , --, X , . :

- , ( ; .. );

, , , .

+7

operator = . ++, , .

KB : http://support.microsoft.com/kb/130486

= , = . = .

, , , , , . " " , , , : , , - .

+7

:

class A
{
public :
   // To workaround LNK2001, comment the following line.
   virtual const A& operator=( const A& f ) = 0;
};

class B : public A
{
public :
   const A& operator=( const A& g ) {return g;}
};

B aB1, aB2;

int /*void*/ main( void )
{
   aB2 = aB1;
}

aB2 = aB1 const A& B::operator=(const A&), B& operator=(const B&);, , , . , , .

+2

All Articles