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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:17:06+00:00 2026-05-26T08:17:06+00:00

I have a problem where I want to clone of a object pointer when

  • 0

I have a problem where I want to clone of a object pointer when doing a deep copy.
like I have T* t1 and I want to create a new object pointer T* t2 in a way that *t1.x= *t2.x.

Is it a good Idea to write a copy constructor which will work like:

T(const T* cpy)
{
   m_var = (*cpy).m_var;
}

T* t1 = new T;
T* t2(t1);

what things should I take care of if using the above approach?

Thanks
Ruchi

  • 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-26T08:17:07+00:00Added an answer on May 26, 2026 at 8:17 am

    To do this you should write a normal copy-constructor and use it like this:

    T(const T& cpy)
    : m_var(cpy.m_var) // prefer initialization-list, thanks to @Loki Astari
    {}
    
    T* t1 = new T;
    T* t2 = new T(*t1);
    

    In the code you show, T* t2(t1); would never call the constructor you have declared (which, by the way, is not a copy-constructor), because it simply initializes the pointer t2 to the value of the pointer t1, making both point to the same object.

    As @Nawaz notes, this copy-constructor is equivalent to the one generated by the compiler, so you don’t actually need to write it. In fact, unless you have any manually managed resources (which, usually, you shouldn’t) you will always be fine with the compiler generated copy-constructor.

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

Sidebar

Related Questions

I have a class that i want to push_back into a deque. The problem
Background I am trying to create a copy of a business object I have
I have problem with return statment >.< I want to store all magazine names
I have a little Java problem I want to translate to Python. Therefor I
I have a problem with stopping a service and starting it again and want
I have small problem with my .net 2.0 winforms application. I want to embed
I still have a problem with the splash screen. I don't want to use
I have a simple, real life problem I want to solve using an OO
I have a small problem with a script. I want to have a default
I have a small problem with how should i think a... problem. I want

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.