开发者

Copy Constructor for pointers to objects

开发者 https://www.devze.com 2023-02-21 21:14 出处:网络
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.

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(){
    G开发者_开发技巧1<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?


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.


You can't make a "constructor for a pointer', but you can make a class that acts like a pointer, and use it instead. They're called "smart pointers", and it's a big enough topic that I can't teach everything here. But there's a simple class named auto_ptr in the standard library you should read about, and the boost library also contains a number of smart pointer classes. You want to use those instead of 'real' pointers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号