I'm interested in improving my understanding of how to avoid writing a C++ class that causes problems when copied.
In particular, I've written a class named Policy
that I intend to copy. I have not defined a non-default destructor, copy constructor, or copy assignment operator. The only operator I've overloaded is the following:
friend bool operator== (const Policy& p1, const Policy& p2) {
for(int i=0; i<p1.x.size(); i++) {
if(p1.x[i] != p2.x[i])
return false;
}
return true;
}
The class's members are either standard data types such as int
, double
, bool
, std::string
, std::vector<double>
, std::vector<int>
, std::vector<string>
, or one of a few small (and thus not overly complex) classes I defined that are definitely copyable without any problem. Now, there is a member of Policy
that is an instance of the NRRan
class, which is a class I built as a wrapper around a non-copyable third-party data type; NRRan
is as follows:
class NRRan {
public:
double doub() { return stream->doub(); }
int intInterval(const int& LB, const int& UB) { return LB+int64()%(UB-LB+1); }
void setNewSeed(const long& seed) {
delete stream;
stream = new Ranq1(seed);
}
NRRan() { stream = new Ranq1(12345); }
~NRRan() { delete stream; }
NRRan(const NRRan& nrran) {
stream = new Ranq1(12345);
*stream = *(nrran.stream);
}
NRRan& operator= (const NRRan& nrran) {
if(this == &nrran)
return *this;
delete stream;
stream = new Ranq1(12345);
*stream = *(nrran.stream);
return *this;
}
private:
Ranq1* stream; // underlying C-struct
Ullong int64() { return stream->int64(); }
};
But the whole point of the NRRan
class is to make Ranq1
copyable. So, given the Policy
class as I've described it (sorry, I am unable to post a large part of the code), is there any thing that might cause a problem when I copy Policy
? My expectation is that copying will create a perfect value-for-value copy.
A more general way of asking my question is the following: Generally speaking, what types of things can possibly cause problems when copying a class? Is there anything in addition to the "Rule of Three" (or "Rule of Five") to be concerned about when making a class copyable?