【问题标题】:Why typecasting operator doesn't work with inherited class?为什么类型转换运算符不适用于继承的类?
【发布时间】:2011-10-14 03:11:02
【问题描述】:

我有两个班级:

struct B {};
struct D {
  operator B& ();
};

当我这样做的时候;

b = d; // B b; ... D d;

结果与调用 D::operator B&() (Demo) 的预期一致。

如果D改为,

struct D : B {
  operator B& ();
};

然后D::operator B&() 不会被调用 (Demo)。 B::B(const B&) 是在 D 然后 D::operator B&() 找到更好的候选人吗?

【问题讨论】:

    标签: c++ inheritance casting operator-overloading


    【解决方案1】:

    如果D 派生自B,则存在从DB 的隐式自动转换。这比用户定义的转换运算符具有更高的优先级。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2017-02-26
      • 1970-01-01
      相关资源
      最近更新 更多