在派生类之间复制构造函数

发布于 2024-11-25 16:28:42 字数 1099 浏览 0 评论 0原文

如何将派生类复制到另一个派生类?

我在术语上有缺陷,所以我将尝试用一个例子来说明。

我们正在与计算机玩家和人类玩家一起玩纸牌游戏。卡牌和命令是其他类别。

class Player
{
    Card *Hand[4];
    // etc...
};

class Human: public Player
{
    Command getCommand();
    void PlayCard(Card card);
    void quit();
    // etc...
};

class Computer: public Player
{
    Command ai();
    void PlayCard(Card card);
    // etc...
};

在主函数的某个地方我们有...

// ...
Human p1; // Assume initialized and usable.
if(p1.getCommand() == QUIT)
{
    cout << "PLAYER 1 RAGEQUITS WHAT A NOOB LOL << endl;
    cout << "A COMPUTER WILL NOW TAKE OVER." << endl;
    p1.quit()
    p1 = new Computer(); // THE IDEA BEING THAT WE WANT TO PRESERVE p1's MEMBERS.
}
// ...

我想做的是将 p1 转换为“计算机”,同时保留其成员的状态。

我们是否使用复制构造函数来做到这一点?如果没有,您使用什么方法?

编辑:这是使用赋值运算符的方法吗?

Computer& Human::operator=(const Human &h) // Assignment operator
{
    Hand = h.Hand;
    member2 = h.member2;
    member3 = h.member3;
    ...

    return *this;
}

我们需要删除/释放主体中的任何内容吗?

How do you copy a derived class to another?

I'm terminologically deficient, so I'll try to illustrate with an example.

We are playing a card game with a computer player and human player. Card and Command are other classes.

class Player
{
    Card *Hand[4];
    // etc...
};

class Human: public Player
{
    Command getCommand();
    void PlayCard(Card card);
    void quit();
    // etc...
};

class Computer: public Player
{
    Command ai();
    void PlayCard(Card card);
    // etc...
};

And somewhere in the main function we have ...

// ...
Human p1; // Assume initialized and usable.
if(p1.getCommand() == QUIT)
{
    cout << "PLAYER 1 RAGEQUITS WHAT A NOOB LOL << endl;
    cout << "A COMPUTER WILL NOW TAKE OVER." << endl;
    p1.quit()
    p1 = new Computer(); // THE IDEA BEING THAT WE WANT TO PRESERVE p1's MEMBERS.
}
// ...

What I am trying to do is converting p1 to a "Computer" while preserving the state of its members.

Do we use a copy constructor to do this? If not, what methods do you use?

EDIT: Is this the way to use the assignment operator?

Computer& Human::operator=(const Human &h) // Assignment operator
{
    Hand = h.Hand;
    member2 = h.member2;
    member3 = h.member3;
    ...

    return *this;
}

Do we need to delete/free anything in the main?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少钕鈤記 2024-12-02 16:28:42

你这里有一个设计问题。如果您想将玩家从人类切换为计算机,同时维护公共成员变量,那么您应该以这种方式构建您的类。

class Player
{ 
public:
    friend class Human;    // These friends are necessary if the controllers
    friend class Computer; // need access to Player's private data.

    Card hand[4];
    Controller* controller;
};

class Controller
{
public:
    virtual Command getCommand(Player const&) = 0;
};

class Human : public Controller
{
public:
    Command getCommand(Player const&) { /* get command from user input */ }
};

class Computer : public Controller
{
public:
    Command getCommand(Player const&) { /* get command from AI */ }
};

然后,当您需要从人类切换到计算机时,只需更改控制器即可。

p1->controller = new Computer();

这样,卡牌将保持不变,仅改变控制机制。

You have a design problem here. If you want to switch a player from a Human to a Computer while maintaining the common member variables then you should structure your classes in that way.

class Player
{ 
public:
    friend class Human;    // These friends are necessary if the controllers
    friend class Computer; // need access to Player's private data.

    Card hand[4];
    Controller* controller;
};

class Controller
{
public:
    virtual Command getCommand(Player const&) = 0;
};

class Human : public Controller
{
public:
    Command getCommand(Player const&) { /* get command from user input */ }
};

class Computer : public Controller
{
public:
    Command getCommand(Player const&) { /* get command from AI */ }
};

Then, when you need to switch from Human to Computer, just change the controller.

p1->controller = new Computer();

This way, the cards will be maintained, only the control mechanism will be changed.

一城柳絮吹成雪 2024-12-02 16:28:42

复制构造函数用于将现有对象复制为同一类的新实例。

您需要的是一个赋值运算符 (operator=),它允许将不同类型的值分配给您的现有类对象。

Copy constructor is used to copy an existing object a new instance of the same class.

What you need is an assignment operator (operator=), which allows assigning the value of a different type to your existing class object.

梦幻之岛 2024-12-02 16:28:42

它不是一个复制构造函数,而是一个转换构造函数。类似于:

Computer::Computer(const Player& had_to_go) : Player(had_to_go) {}

这将使用Player的复制构造函数来保留公共基类中的成员。

当然,你最好让 Player::Player(const Player&) 正常工作,遵循 “三法则” 等等。

最后,你会做类似的事情:

p1.quit();
Computer* replacement = new Computer(p1);
delete p1;
p1 = replacement;

It wouldn't be a copy constructor, but a converting constructor. Something like:

Computer::Computer(const Player& had_to_go) : Player(had_to_go) {}

This will use Player's copy constructor to preserve the members in the common base class.

Of course, you'd better make Player::Player(const Player&) work right, following the "rule of three" and all.

In the end, you'd do something like:

p1.quit();
Computer* replacement = new Computer(p1);
delete p1;
p1 = replacement;
掀纱窥君容 2024-12-02 16:28:42

您可以使用构造函数。对于这种特殊情况,我可能有一个名为“CopyGameState”的方法。

You could use a constructor. For this particular case I'd probably have a method called something like "CopyGameState".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文