| navderm (93) | |
|
Hi All, Dumb C++ question, I wanted your view on Suppose class A { public: // constructor // copy constructor A(const A& a) { this->x = a.x; this->y = a.y; this->z = a.z; } private: double x, y, z; } Does this mean that if some third person has the definition of class, he can virtually copy all the elements from a given object of A into new object of A and then use it ? Does this not be a problem when I don't want someone else be able to copy it this way but just me ? | |
|
|
|
| Cubbi (1928) | ||||
Yes, this class is copyable, like the majority of C++ classes.
Then you can 1) document your desire (e.g. std::FILE, better known as the C library's FILE, is copyable, but it's documented that using a copy for I/O yields undefined behavior) 2) delete the copy constructor (e.g. std::fstream, std::thread, and other movable, but not copyable classes)
You the library writer who is designing this class? 3) make the copy constructor private, so that it would only be called from those member functions you wrote. C++ works best with copyable types, though, anything else requires significant justification. | ||||
|
|
||||