gpt4 book ai didi

c++ - std::string x(x);

转载 作者:可可西里 更新时间:2023-11-01 16:30:02 25 4
gpt4 key购买 nike

std::string x(x);

这在我的编译器上严重崩溃。这是否意味着我应该在我自己的复制构造函数中测试 this != &that,或者我是否可以假设没有客户会如此愚蠢?

最佳答案

您不应该针对试图严重崩溃的代码进行测试。参见 Null References .它说

"Just as you must assume that a non-null pointer is valid, you must assume that a reference is valid. You must have faith in your fellow programmers."

我要补充

... you must assume that the source of a copy is valid.

如果您“解决”了您的案例,那么该怎么做?

string x = string(x);

关于c++ - std::string x(x);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2529111/

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