C++隐式类型转换
C++ implicit typecast
我想将许多相似的类相互转换。它们至少有一个共同的抽象祖先,它定义了 2 种基本方法。
我遇到了奇怪的类型转换错误,所以我做了一个简化的例子。在层次结构的顶部:Integer 类。它是一个具有int val()
方法的抽象类。其中一个子值只是物理int
值的持有者,而另一个引用 2 个整数,val()
是其两个引用的整数的总和。
我写了这段代码,我不明白为什么注释表达式在使用临时变量时无法编译,效果很好。
class Sum;
class Integer {
public:
virtual int val(void) const = 0;
Sum operator+(Integer & other);
};
class Sum : public Integer {
private:
Integer &op1, &op2;
public:
explicit Sum(Integer &a, Integer &b) : op1(a), op2(b) {};
int val(void) const {return op1.val() + op2.val();};
};
class Int : public Integer {
private:
int v;
public:
Int(int value=0) : v(value) {};
Int(Integer & other) : v(other.val()) {};
int val() const {return v;};
Int & operator=(Integer & other){v = other.val(); return *this;};
Int & operator=(int value){v = value; return *this;};
};
std::ostream & operator<<(std::ostream & out, Integer & i){return out << i.val();}
Sum Integer::operator+(Integer & other){return Sum(*this, other);}
int main(int argc, const char **argv){
Int a=42, b=57;
// Int s = a+b; => conversion from ‘Sum’ to non-scalar type ‘Int’ requested
Sum r = a+b;
Int s = r; /* OK */
cout << a << " + " << b << " = " << s << endl;
return 0;
}
class Int : public Integer {
private:
int v;
public:
Int(int value=0) : v(value) {};
Int(Integer & other) : v(other.val()) {};
int val() const {return v;};
Int & operator=(Integer & other){v = other.val(); return *this;};
Int & operator=(int value){v = value; return *this;};
};
构造函数Int(Integer & other)
不会修改其参数,因此可以(应该)将该引用const
:
Int(Integer const& other) : v(other.val()) {};
这也解决了您的问题:
Sum Integer::operator+(Integer & other);
Int s = a+b;
operator +
(可以说应该是一个自由函数而不是成员函数)返回一个 Sum
类型的 prvalue/temporary 。此临时不能绑定到非常量左值引用,因此不能使用构造函数Int(Integer & other)
。
类似地,对于Int & operator=(Integer & other)
,常量引用就足够了。
对于接受非常量引用的函数,例如 Int 的构造函数,不能传递临时对象。 对此的一个常见解释是,如果函数采用非常量引用,则允许修改引用,但是对于临时对象,此更改实际上不会发生,因为引用变量在函数调用之外无法访问。
正如 DyP 在注释中建议的那样,将值更改为 const 将提供一个解决方案,或者您可以简单地将其绑定到变量,就像您使用 'Sum r = a+b' 所做的那样。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++中的双指针类型转换
- 逐位操作的隐式类型转换
- 模板中的类型转换
- 在 C++(和 C)中进行类型转换时明显不一致
- 字符类型转换不兼容
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- C++:用户定义的显式类型转换函数错误
- 将类指针类型转换为键时出错
- 通过引用传递参数时C++类型转换
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- C++显式类型转换(C 样式强制转换)的强制表示法和static_cast的多种解释
- C++无效的函数类型转换
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 事件系统:使用类型转换或联合进行继承
- 如何在参数中定义隐式类型转换的构造函数?
- 类模板实例化中的类型转换