为什么我的复制构造函数不起作用?

Why doesn't my copy constructor work?

本文关键字:不起作用 构造函数 复制 我的 为什么      更新时间:2023-10-16

这可能很容易,但我有点困惑。我遇到了一些问题,我的矢量表现不好,现在看来我找到了罪魁祸首。这是我的Player类的淡化版本。

class Player {
private:
    std::string _firstName;
    std::string _lastName;
public:
    Player(std::string firstName, std::string lastName) {
        _firstName = firstName;
        _lastName = lastName;
    };
    Player(const Player& otherPlayer) {
        _firstName = otherPlayer._firstName.c_str();
        _lastName = otherPlayer._lastName.c_str();
        std::cout << "Created " << _firstName << " " << _lastName << std::endl; // Why doesn't _firstName and _lastName contain anything?
    };
    std::string GetName() { return _firstName + " " + _lastName; };
};
int main(int argc, const char * argv[])
{
    Player player1 = Player("Bill", "Clinton");
    Player player2 = Player(player1);
    std::cout << "Player: " << player2.GetName() << std::endl;
    return 0;
}

输出是微薄的Player:。我不知道为什么我的复制构造函数没有做我希望它做的事情,特别是考虑到这样的建议(Zac Howland的评论解释了c_str();部分)。我是否违反了三条规则(顺便说一句,我还没有完全清醒过来)?如果有人能给我指明正确的方向,我将不胜感激!

它适用于我:http://ideone.com/aenViu

我刚刚添加了:

#include <iostream>
#include <string>

但有一件事我不明白:

_firstName = otherPlayer._firstName.c_str();
_lastName = otherPlayer._lastName.c_str();

为什么选择.c_str()?您将string转换为char*以将其分配给新的string

EDIT:Zac Howland在评论中指出:"在C++11之前,如果你想确保你的字符串被复制(而不是引用计数),你必须使用c_str()方法来强制它复制字符串。新标准消除了这一点,但如果他使用的是旧的编译器,或者还没有完全实现C++11的编译器,它将确保深度复制。"

只需:

_firstName = otherPlayer._firstName;
_lastName = otherPlayer._lastName;

而且,你真的需要这个复制构造函数吗?默认会做你想做的事我想。。。


此外,不是分配成员:

Player(std::string firstName, std::string lastName) {
    _firstName = firstName;
    _lastName = lastName;
}

请改用成员初始化列表

Player(std::string firstName, std::string lastName) :
    _firstName( std::move(firstName) ),
    _lastName( std::move(lastName) )
{}

在第一种情况下,调用字符串的默认构造函数,然后调用字符串的复制赋值运算符,与直接调用复制构造函数的第二种情况相比,肯定会有(轻微的)效率损失。

最后一件事,在可能的情况下,不将值作为方法参数传递,传递引用,甚至在不需要修改时传递常量引用:

Player( const std::string& firstName, const std::string& lastName )
//      ^^^^^            ^            ^^^^^            ^
    : _firstName( firstName )  // no move here, since args are constant references
    , _lastName( lastName )
{}

所有修改的工作实例。

相关文章: