c++ - Why does an overloaded assignment operator not get inherited? -


why code:

class x { public:     x& operator=(int p) {         return *this;     }     x& operator+(int p) {         return *this;     } };  class y : public x { };  int main() {     x x;     y y;     x + 2;     y + 3;     x = 2;     y = 3; } 

give error:

prog.cpp: in function ‘int main()’: prog.cpp:14:9: error: no match ‘operator=’ in ‘y = 3’ prog.cpp:14:9: note: candidates are: prog.cpp:8:7: note: y& y::operator=(const y&) prog.cpp:8:7: note:   no known conversion argument 1 ‘int’ ‘const y&’ prog.cpp:8:7: note: y& y::operator=(y&&) prog.cpp:8:7: note:   no known conversion argument 1 ‘int’ ‘y&&’ 

why + operator inherited, = operator not?

class y contains implicitly-declared assignment operators, hide operator declared in base class. in general, declaring function in derived class hides function same name declared in base class.

if want make both available in y, use using-declaration:

class y : public x { public:     using x::operator=; }; 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

c++ - How to test/verify that zero-initialization takes place -

node.js - Webpack/TypeScript Raising Errors When node_modules is Excluded -