Tuesday, March 26, 2019

c++ - Assignment operator with derived type argument

class A {
private:
A& operator=(const A&);
};

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

};


int main() {

B b1;
B b2;

b1 = b2;


return 0;
}


This gives error on compilaton:



test.cpp: In member function 'B& B::operator=(const B&)':
test.cpp:16:5: error: 'A& A::operator=(const A&)' is private
test.cpp:19:20: error: within this context
test.cpp: In function 'int main()':

test.cpp:31:7: note: synthesized method 'B& B::operator=(const B&)'
first required here
Build error occurred, build is stopped


Since B::operator=(A&) has a non-standard signature, the compiler generates it's own B::operator=(B&) which (tries) to call A::operator(A&), which is private.



Is there any way I can get the compiler to use B::operator=(A&) also for B arguments?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...