C ++隐式类型转换(C++ implicit typecast)

编程入门 行业动态 更新时间:2024-10-28 20:24:45
C ++隐式类型转换(C++ implicit typecast)

我想将许多类似的类转换成彼此。 它们至少有一个共同的抽象祖先,它定义了两种基本方法。

我遇到了奇怪的类型转换错误,所以我做了一个简化的例子。 在层次结构的顶部: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; }

I want to convert numerous similar classes into each other. They have at least a common abstract ancestor, which defines 2 basic methods.

I ran into weird typecast errors, so I made a simplified example. On top of the hierarchy: the Integer class. It's an abstract class that have an int val() method. One of it's child is just a holder for a physical int value, while the other references 2 Integers and val() is the sum of its two referenced Integers.

I wrote this code, and I could not figure why the commented expression fails to compile while using a temporary variable works just great.

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。 此临时不能绑定到非const左值引用,因此不能使用构造函数Int(Integer & other) 。

类似地,对于Int & operator=(Integer & other) ,const引用就足够了。

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;}; };

The constructor Int(Integer & other) doesn't modify its argument, so could (should) make that reference const:

Int(Integer const& other) : v(other.val()) {};

This also solves your problem:

Sum Integer::operator+(Integer & other); Int s = a+b;

The operator + (which should arguably be a free function instead of a member function) returns a prvalue/temporary of type Sum. This temporary cannot bind to a non-const lvalue reference, therefore the constructor Int(Integer & other) cannot be used.

Similarly for Int & operator=(Integer & other), a const reference is sufficient.

更多推荐

本文发布于:2023-08-06 08:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1447794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   隐式   typecast   implicit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!