Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:01:11+00:00 2026-05-24T00:01:11+00:00

How do you copy a derived class to another? I’m terminologically deficient, so I’ll

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:01:12+00:00Added an answer on May 24, 2026 at 12:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see the following pattern in an MVC app where basically a derived class
I have base-class Base from which is derived Derived1 , Derived2 and Derived3 .
I have two classes - base class A and derived class B - and
The question regards .Net specifically. Suppose a base class A has 2 derived classes
I have a class hierarchy like this: ClassA inherits from NSObject ClassB inherits from
I have a class that implements IAuthorizationPolicy. I set up a custom Principal object
Does the compiler produce the same IL if I make a class with public
[Edit: don't try to understand the whole thing and don't waste your time to
I want to make sure that *this != &rhs in the assignment operator. But
This is my first post, I'm new to this site, but I've been lurking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.