why if we have pure virtual assignment operator in a base class, then we implement that operator on the derived class, it give linker error on the base class?
currently I only have the following explanation on http://support.microsoft.com/kb/130486 , it said that the behavior is by design since normal inheritance rules does not apply.
it is not clear for me, why is it generate linker error by design? can someone give me more clear explanation about this?
edit: added my simplified code of which the error occured:
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;
}
the code will compile with no errors without __declspec(dllexport)
and/or without pure virtual operator= declaration on base class.
without __declspec(dllexport)
after assignment of *bd1 = *bd2;
, d1::memberB is 1, but with __declspec(dllexport)
d1::memberB is left unchanged
with __declspec(dllexport)
, and without pure virtual declaration, after assignment of *bd1 = *bd2;
, d1::memberB is left unchanged
From section 12.8 of the standard:
13 The implicitly-defined copy assignment operator for class X performs memberwise assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list, and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. Each subobject is assigned in the manner appropriate to its type:— if the subobject is of class type, the copy assignment operator for the class is used (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
The subclass is using the implicitly-defined copy assignment operator, and there is no definition of the base class's copy assignment operator, but it is declared, so you get a link error instead of a compilation error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With