在派生类之间复制构造函数
如何将派生类复制到另一个派生类?
我在术语上有缺陷,所以我将尝试用一个例子来说明。
我们正在与计算机玩家和人类玩家一起玩纸牌游戏。卡牌和命令是其他类别。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你这里有一个设计问题。如果您想将玩家从人类切换为计算机,同时维护公共成员变量,那么您应该以这种方式构建您的类。
然后,当您需要从人类切换到计算机时,只需更改控制器即可。
这样,卡牌将保持不变,仅改变控制机制。
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.
Then, when you need to switch from Human to Computer, just change the controller.
This way, the cards will be maintained, only the control mechanism will be changed.
复制构造函数用于将现有对象复制为同一类的新实例。
您需要的是一个赋值运算符 (
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.它不是一个复制构造函数,而是一个转换构造函数。类似于:
这将使用
Player
的复制构造函数来保留公共基类中的成员。当然,你最好让
Player::Player(const Player&)
正常工作,遵循 “三法则” 等等。最后,你会做类似的事情:
It wouldn't be a copy constructor, but a converting constructor. Something like:
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:
您可以使用构造函数。对于这种特殊情况,我可能有一个名为“CopyGameState”的方法。
You could use a constructor. For this particular case I'd probably have a method called something like "CopyGameState".