Say you had a class in C++ like below:
template<typename T>
class my_class {
public:
private:
std::set<T*> class_set;
int elements_in_set;
}
I'm trying to write a copy constructor for this class, and so far I'm stuck at the following, where I only know how to copy over the integer elements_in_set
, but I'm not sure how to copy over the set, and do a deep copy of the things that other's class_set
's pointers are pointing to:
my_class(my_class const& other)
: class_set{}, elements_in_set{other.elements_in_set} {
//don't know how to copy over the set
}
I know that if the class has a pointer as a member, you'd use new data type[size of data pointer points to]
, but do you have to call new
to make the set itself, and then new
for every pointer in the set? Any help would be greatly appreciated. It would also be nice if there was a way to do this using standard library algorithms rather than for
loops.