gpt4 book ai didi

c++ - 为什么首先重载 operator= ?

转载 作者:行者123 更新时间:2023-11-30 00:50:43 24 4
gpt4 key购买 nike

<分区>

在 C 中,我们可以将一个结构分配给另一个结构:

struct vector {
int x, y;
};

int main (void) {
struct vector v1 = {1, 1}, v2;
v2 = v1;
return 0;
}

对于 C++ 对象也是如此:

class vector {
public:
int x, y;
vector (int a = 0, int b = 0) : x(a) , y(b){};
};

int main (void) {
vector v1(1, 1), v2;
v2 = v1;
return 0;
}

除了将指针作为成员变量的类之外,我们不想分配内存地址而是值,或者可能分配来自不同类的对象 - 为什么我们要重载 = 运算符首先?在这个至关重要的时代,什么是一个例子? (而不是上面的例子?)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com