ful*_*ton 2 c++ inheritance operators conversion-operator
假设我有一个继承自Animal类的类Dog,你可能想在Dog :: operator =中插入对Animal :: operator =的调用.
什么是最可读/最常用的写作方式?
我想我知道那两个......
static_cast<Animal*>(this)->operator=(other);
Run Code Online (Sandbox Code Playgroud)
和
this->Animal::operator=(other);
Run Code Online (Sandbox Code Playgroud)
AnT*_*AnT 12
因为你是在子类方法中完成的
Animal::operator=(other);
Run Code Online (Sandbox Code Playgroud)
没必要this->.范围解析语法完全按照请求执行.我没有看到与演员"间接"做到这一点.
另请注意,在一般情况下,使用强制转换执行此操作可能不会产生预期结果,因为它不会禁用虚拟方法调用的动态解析.(而且,BTW,赋值运算符可以声明为虚拟).一个明显的结果是,使用虚拟方法,"强制转换"变体可能很容易导致无休止的递归.
| 归档时间: |
|
| 查看次数: |
663 次 |
| 最近记录: |