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

  • SEARCH
  • Home
  • 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 8142063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:42:07+00:00 2026-06-06T12:42:07+00:00

All, this seems like a simple problem but it’s giving me fits. Lets say

  • 0

All, this seems like a simple problem but it’s giving me fits. Lets say I have a C++ base class called Animal and derived classes Cat, Dog, Horse, Hippo, Snake. I have another class called Zoo that contains a list (Qt List in this case where the base class Animal is a QObject) of Animal objects. Now what I want to do is create a Zoo object and be able to copy it to another Zoo object and have the destination Zoo have it’s own copy of the Animals. The problem is in the Zoo copy constructor because it thinks it has a list of Animals it calls the copy constructor for Animal instead of Dog, Cat, etc. as compilers are ought to do. To get around this I changed the Animal list to be a list of Animal pointers and then created my own Animal copy constructor and assignment operator. Each of these calls the Animal::Clone( Animal &rhs ) method that in turn calls each derived class of Animals Clone() method that returns a new pointer allocated from the heap with a copy of its data. This all works fine but I keep thinking that I’m missing a more elegant solution. So my question is, when you have a container with objects of a type how do we copy the class that contains the container? I hope that makes sense.

  • 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-06-06T12:42:09+00:00Added an answer on June 6, 2026 at 12:42 pm

    What you did is almost the idiomatic way of doing it in C++. The virtual clone() method is in fact known as the virtual copy constructor idiom. Typically, you’d declare it as a virtual abstract method of the following signature.

    Animal * clone() const = 0;
    

    Note that it doesn’t take any rhs parameter, it simply returns a clone of itself. Given an instance of an animal Animal * myPet, you can get a duplicate one by saying

    Animal * mySecondPet = myPet->clone();
    

    You’re forced to implement it in the derived classes if they are to be instantiated.

    [Ed:] As you’ve found out, it is impossible for a container class to hold the items directly and not through pointers, due to the slicing problem inherent in the design of C++.

    In C++, your solution is an idiom: it’s as elegant as it gets, and everyone who sees it, who knows their C++, should immediately know what you mean. Idioms and certain design patterns are part of the language. You can’t call yourself proficient in a language, whether human or programming one, without knowing the idioms.

    In many of my projects, I use an interface class (all abstract virtual methods), called Cloneable, that consists just of this clone method. It’s easy then to enforce some base class to be cloneable: just inherit from Cloneable. I wish that QEvent was Cloneable that way, for example. As it is, it’s impossible to duplicate QEvents to post them to multiple event queues.

    Daniel’s suggestion “Have you tried making the copy constructor for Animal virtual?” can’t be taken literally. There’s no such thing as virtual copy constructors, or any constructors, really, in C++ – for a simple reason: before an object is constructed, its virtual method table is not finalized. Specifically, the code in Animal’s constructor will execute before any derived class’s constructors get called to swap out the virtual method table pointer to the one in the derived class. So, if you made any virtual method calls in the constructor, they won’t go to any class that’s derived from your class. That’s it.

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

Sidebar

Related Questions

This seems like a simple problem but I can't seem to find the answer.
I have what seems like a simple problem, but searching the net hasn't yielded
I have what seems like it should be a really simple problem, but somehow
So this seems like a simple little problem, but I haven't been successful with
So this seems like it's a very simple problem to anyone at all conversant
So this seems like such a simple problem but i am struggling with it.
I know this seems like a repeat question, but i've read all the others
hello all this seems to be my problem I have a table in mysql
This seems like an easy problem to solve, but it's kickin my ass. The
Seems like a simple problem: I have an SVN repo inside our firewall. I

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.