gpt4 book ai didi

c++ - 复制构造函数和重载的加法运算符

转载 作者:太空狗 更新时间:2023-10-29 20:51:18 25 4
gpt4 key购买 nike

我正在审查 C++ 中的运算符重载。只是为了好玩,我正在实现一个 BigInt 类。

我要为其重载的第一个运算符是加法运算符。我已决定将此运算符重载为友元非成员函数。这是此代码的 MWE:

#include <cassert>
#include <iostream>
#include <string>

class BigInt{
public:
friend BigInt operator+(const BigInt &bi1, const BigInt &bi2);

BigInt() {}
explicit BigInt(const std::string &in) {
if (in.size() != 0) {
for (auto cc = in.rbegin(); cc != in.rend(); ++cc) {
value_.push_back(*cc);
}
}
}
std::string value() {
std::string actual_value{}; // Reversed string.
for (auto cc = value_.rbegin(); cc != value_.rend(); ++cc) {
actual_value.push_back(*cc);
}
return actual_value;
}

private:
std::string value_; // String of digits as characters.
};

BigInt operator+(const BigInt &bi1, const BigInt &bi2) {
BigInt result{};

result.value_ = "4421";
return result;
}

int main() {
std::cout << "Test addition operator... ";
std::string number{"1234"}; // Number 1,234.
BigInt mm(number);
std::string number_ten{"10"}; // Number 10.
BigInt nn(number_ten);

BigInt mm_nn = mm + nn;

std::string expected_result{"1244"}; // 1,234 + 10 = 1,244.
assert(mm_nn.value() == expected_result);
std::cout << "ok." << std::endl;
}

这段代码模拟了加法的行为。它编译并运行。然而,当我为 BigInt 类添加复制构造函数时,此代码停止工作。 IE。如果我将其添加到类声明中:

显式 BigInt(const BigInt &in): value_(in.value_) {}

代码甚至无法编译。编码的加法函数返回 BigInt 构造实例的拷贝。为此,必须定义一个复制构造函数。如果我不自己定义它,那么编译器会这样做。编译器产生了什么我没有使用添加的复制构造函数产生的?这是我得到的编译错误:

$ g++ -std=c++14 -g mwe.cpp 
mwe.cpp: In function ‘BigInt operator+(const BigInt&, const BigInt&)’:
mwe.cpp:34:10: error: no matching function for call to ‘BigInt::BigInt(BigInt&)’
return result;
^
mwe.cpp:9:3: note: candidate: BigInt::BigInt()
BigInt() {}
^
mwe.cpp:9:3: note: candidate expects 0 arguments, 1 provided
mwe.cpp: In function ‘int main()’:
mwe.cpp:44:23: error: no matching function for call to ‘BigInt::BigInt(BigInt)’
BigInt mm_nn = mm + nn;
^
mwe.cpp:9:3: note: candidate: BigInt::BigInt()
BigInt() {}
^
mwe.cpp:9:3: note: candidate expects 0 arguments, 1 provided

由此看来,编译器似乎需要一个我没有提供的复制构造函数。现在...如果我删除 explicit 关键字,一切正常。但是,我已经看到带有显式复制构造函数的实现,例如:Explicit copy constructor

我错过了什么?为什么我不能在重载加法运算符时使这个复制构造函数显式化?一般来说,复制构造函数应该显式化吗?

最佳答案

使复制构造函数显式没有意义。将其删除。

BigInt(const BigInt &in): value_(in.value_) {}

explicit 复制构造函数的概念问题

使复制构造函数显式使得不可能从函数返回对象。

让我们将您的代码简化为以下内容:

struct BigInt
{
BigInt() {}
explicit BigInt(const BigInt &in) {}
};

BigInt operator+(const BigInt &bi1, const BigInt &bi2)
{
BigInt result;
return result;
}

int main() {}

return result 行中,编译器依赖复制构造函数返回一个对象。如果复制构造函数是显式的,则无法将 BigInt 构造为返回值。

尝试使用:

BigInt operator+(const BigInt &bi1, const BigInt &bi2)
{
BigInt result;
return BigInt(result);
}

是徒劳的,因为这等同于:

BigInt operator+(const BigInt &bi1, const BigInt &bi2)
{
BigInt result;
BigInt result1(result);
return result1;
}

无论您在函数中做什么,问题仍然存在。

关于c++ - 复制构造函数和重载的加法运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50786776/

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