我在为对象指针编写复制构造函数时遇到问题。
这正是我的问题
,我有一个类 G1,它有一个对象 s1 作为其私有数据成员。这是一个结构体的对象。
该结构由一个向量>组成。
现在,当我为 G1 创建指针时,一切都很好。
当我尝试将此指针复制到同一类的另一个新指针时,它正在制作浅复制。
因此,当我尝试删除第一个指针时,第二个指针将失去其引用。
我的代码是这样的。
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 类,它仅具有值类型并且不需要用户定义的复制构造函数。
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 中调试时,我看不到任何复制构造函数或赋值运算符函数被调用。
类中需要创建深拷贝的指针成员很简单。当在其他类中声明指针时,我感到很困惑,而该类的对象正在需要为其创建深层复制的类中使用。
有人可以提供一些关于如何在上述情况下进行深层复制的提示吗?
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?
发布评论
评论(2)
指向对象的指针不是指向对象——它只是它的地址。您可以将其视为对对象的引用。复制指针不会复制对象 - 你必须始终显式地复制它,例如。调用复制构造函数
如果你想获得类的新副本以及每个指向它的指针(不知道你可以用它做什么,但为什么不;)那么你可以像上面那样做,或者编写一些类似指针的类,在复制指针上复制对象- 我不认为这是一个实现这种行为的现成库。
如果您想使用同一个对象,无论您创建了多少个指向它的指针,那么您应该考虑 std::auto_ptr 或其他库中的类似智能指针类。智能指针将为您释放对象,因此您根本不需要调用删除。
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
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.
你不能创建一个“指针的构造函数”,但你可以创建一个像指针一样的类,并使用它来代替。它们被称为“智能指针”,这是一个足够大的主题,我不能在这里教所有内容。但是您应该阅读标准库中有一个名为 auto_ptr 的简单类,并且 boost 库还包含许多智能指针类,而不是“真正的”指针。
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.