Trouble with inheritance of operator= in C++

前端 未结 5 1564
灰色年华
灰色年华 2020-11-27 04:20

I\'m having trouble with the inheritance of operator=. Why doesn\'t this code work, and what is the best way to fix it?

#include 

class A
{
         


        
5条回答
  •  生来不讨喜
    2020-11-27 04:59

    Normally, operator= is defined in B as

    B& operator=(B const &);
    

    Since B is not an unambiguous and accessible base of 'C', the conversion from C to B is not allowed by the compiler.

    If you really want to have a 'C' be assigned to 'B', 'B' should support an appropriate assignment operator as

    B& operator=(C const &);
    

提交回复
热议问题