小编Joc*_*380的帖子

为什么 C++ 类可以等于一个变量

我有这个代码:

#include <iostream>
using namespace std;

class complex
{
    double re;
    double im;

public:
    complex(): re(0), im(0) {}
    complex(double x) {re = x, im = x;}
    complex(double x, double y) {re=x, im =y;}
    void print() {cout << re << " " << im;}
};

int main()
{
    complex c1;
    double i=2;
    c1 = i;
    c1.print();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么这一行中的代码会编译。

c1 = i;
Run Code Online (Sandbox Code Playgroud)

编译器没有给出错误(或警告),为什么?

c++

2
推荐指数
1
解决办法
116
查看次数

标签 统计

c++ ×1