0

I'm reading book about C++ and came across copy-constructor. So I have a problem understanding this material. In which situation we should write our own functions for copy-control (assignment operator, copy-constructor)?

For example: Should we use here this functions

class TextQuery {
public:
    using lineNo = vector<string>::size_type;
    explicit TextQuery(ifstream&);
    QueryResult query(const string&) const;
private:
    shared_ptr<vector<string>> file;
    map<string, shared_ptr<set<lineNo>>> wm;
};
  • 4
    related/dupe: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – NathanOliver Aug 23 '21 at 14:12
  • 3
  • It looks like `TextQuery` could go with "the rule of zero" since all the member variables handles their own allocations/decallocations. – Ted Lyngmo Aug 23 '21 at 14:16
  • You do it only when your class "owns a resource" that needs manual management, such as a pointer to heap-allocated memory, or `FILE *`, or whatever. But since normally you would use containers and `std::fstream` (which don't need to be manually managed), you almost never need copy operations. – HolyBlackCat Aug 23 '21 at 14:22

0 Answers0