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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:55:29+00:00 2026-05-25T09:55:29+00:00

I am having problem in writing copy constructor for pointers to objects. This is

  • 0

I am having problem in writing copy constructor for pointers to objects.
This is my exact problem

I have a class G1 that has an object s1 as its private data member. This is an object of a struct.

The struct is composed of a vector<pair<int,pointer to another object of a different class>>.

Now when I create a pointer for G1 everything is fine.

When I try to copy this pointer to another new pointer of the same class it is making a shallow copy.

So when I try to delete the first pointer the second loses its reference.

My code goes something like this.

template <typename T,typename V>
class G1{
private:
    S<typename T> s1;
public:
    G1<T,V>(){}
    G1<T,V>(const G1<T,V>& g2):s1(g2.s1){}
};

template<typename T>
struct S{
private:
    vector<pair<int,B<T>*>> smap;
public:
    S<T>(const S& st){
        for(vector<pair<int,B<T>*>>::iterator it = st.getMap().begin(); it!= st.getMap().end(); it++){
            B<T> bptr = new B<T>(*it->second);
            smap.push_back(pair<*(it)->first,bptr>);
        }
    }
};

//ASSUME CLASS B IS PRESENT WHICH JUST HAS VALUE TYPE AND NO USER DEFINED COPY CONSTRUCTOR IS NEEDED.

void main(){
    G1<string,string>* g1= new G1<string,string>;
    //values loaded into g1.

    G1<string,string>* g2= g1;
    delete g1;
    g2.display();  //Code breaks at this point since I am not able to create two pointers pointing different locations.
                // I am not able to make a new copy of the smap vector which has pointers and hence the problem.
}

Any suggestions please.
while doing pointer assignments will the copy constructor be called? While debugging in visual studio I am not able to see any copy constructor or assignment operator function being called.

A pointer member in the class for which deep copy needs to be created is simple. I am getting confused when pointer is declared in some other class whose object is being used in the class for which deep copy needs to be created.

Can somebody provide some hint as to how to make a deep copy in the above case?

  • 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-25T09:55:31+00:00Added an answer on May 25, 2026 at 9:55 am

    Pointer to object is not pointed object – it’s just its address. You can think of it like a reference to an object. Copying pointer will not copy object – you have to make it always explicitly eg. calling copy constructor

    G1<string,string>* g2= new G1<string,string>(g1);
    

    So is there no way for me to create two pointers for G1 class and copy one to other and delete the former and still have the latter? Is there no copy constructors that could be written in this situation?

    If you want to get new copy of the class with each pointer to it (no idea what could you use it for, but why not 😉 then you can do it as bove or write some pointer-like class that copies object on copying pointer – i don’t think theter is an out-of-box library implementing such behavior.
    If you want to use the same object, no matter how many pointers you have created to it, then you should consider std::auto_ptr or similar smart pointer classes from other libraries. Smart pointers will free object for you, so you don’t need to call delete at all.

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

Sidebar

Related Questions

I am having a problem in writing the query for this. I have a
The problem I'm having with writing a web application architecture is that I want
I have been writing a python script and I am having a problem with
I'm having a simple problem when it comes to writing up typeclasses that inherit
I have an assignment that has asked me to copy a file using buffered
I'm having a problem writing Norwegian characters into an XML file using C#. I
I'm writing my first bit of jQuery, and I'm having a problem with jQuery.get()
Background I am trying to create a copy of a business object I have
this is really frustrating me. this problem occurs when I have a particularly large
I am having a problem with a XAML report that needs to be saved

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.