复制构造函数以指向对象 [英] Copy Constructor for pointers to objects

查看:97
本文介绍了复制构造函数以指向对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写指向对象的指针的副本构造函数时遇到问题. 这是我的确切问题

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

我有一个类G1,它具有对象s1作为其私有数据成员.这是结构的对象.

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

结构由vector<pair<int,pointer to another object of a different class>>.

现在,当我为G1创建一个指针时,一切都很好.

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.

我的代码是这样的.

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>);
        }
    }
};

//假设存在B类,仅是值类型且不需要用户定义的COPY构造函数.

//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.
}

请提出任何建议. 在进行指针分配时,将调用副本构造函数吗?在Visual Studio中进行调试时,我看不到任何复制构造函数或赋值运算符函数被调用.

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);

因此,我没有办法为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?

如果您想获得每个指向该类的指针的类的新副本(不知道您可以将其用作什么,但为什么不这样做;),那么您可以像勇敢地做它一样,或者编写一些类似指针的类来复制对象关于复制指针-我不认为这是一个实现此类行为的现成库. 如果要使用同一对象,无论已创建了多少个指针,都应考虑使用std :: auto_ptr或其他库中的类似智能指针类.智能指针将为您释放对象,因此您根本不需要调用delete.

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.

这篇关于复制构造函数以指向对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆