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 175497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:42:34+00:00 2026-05-11T13:42:34+00:00

Update 1: Corrected nonsense code! Thanks for comments, I made a hash of the

  • 0

Update 1:

Corrected nonsense code! Thanks for comments, I made a hash of the first snippet, oops.

Update 2:

Also updated question title, as the use of dynamic_cast has been pointed out as not necessary by answers.

What I’m trying to achieve here is a deep copy using strong types; I want to be able to copy Class2 to another instance of Class2; however, I also want to use the CopyTo function from the Class1 which is the base. This idea comes from my C# experience, where usually I’d just make the return type generic (see C# snippet).

void Class1::CopyTo(Class1 *c1) {     // Write data in to c1 from this instance.     c1->exampleData = exampleData; }  // Class2 inherits Class1 Class2 *Class2::Copy() {     Class2 *c2a = new Class2();     CopyTo(c2a);      Class2 *c2b = dynamic_cast<Class2*>(c2a);     return c2a; } 

And here’s the way I’d so it in C#:

public class Class1 {     T Copy<T>()         where T : Class1     {         /* Can't remember the best way to do this in C#;          * basically if T was Class2 this would need to create          * a new instance of that class, and the same goes for          * Class1. */                  T copy = createNewInstance();          // Copy the data from this to 'copy'.         copy.exampleData = exampleData;          return copy;     } } 

Now, compared to the C# snippet, the C++ snippet feel smelly. Is it possible to do this without pointers, or is this way best practice?

  • 1 1 Answer
  • 3 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. 2026-05-11T13:42:35+00:00Added an answer on May 11, 2026 at 1:42 pm

    I’m not sure what you are trying to achieve because the code still doesn’t make much sense. However, I believe the following should approximate what you’re trying to do. Note that I don’t use heap memory: it’s not necessary and it would leak memory.

    template <typename T> T Class1::Copy() {     T instance;     CopyTo(&instance);     return instance; } 

    This works because you pass a (polymorphic) pointer to instance to the CopyTo method of Class1.

    Then you could call the code like this:

    Class2 x1; // Fill x1 Class2 x2 = x1.Copy<Class2>(); 

    However, this code still smells because it’s no idiomatic C++: In C++, you would usually write a copy constructor instead. Late-bound Copy methods do exist but they are very rarely needed, and the above is not late bound (but neither is your C# code).

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

Sidebar

Related Questions

Update: I've corrected the post, so the question is closed. Expected result: Menu width
-edit- nevermind, i made a mistake in my select statement. Update was working correctly.
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
Important: This question is getting quite long, if this is the first time you're
UPDATE: This problem was corrected by updating to the latest fancybox css. I'm building
UPDATE It seems to have magically corrected itself because now it works, but I
A simplified version of the code is posted below (white space, comments, etc. removed
UPDATE I corrected the problem with the help of the help of the group
Given the code : SQLiteCommand cmd = new SQLiteCommand(UPDATE \Category\ SET \name\=?name, \description\=?description, \color\=?color,
UPDATE!!! Suggested answer is NOT correct, my mistake. The #container DIV should've had float:left;.

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.