Posts

Showing posts from January, 2019

Times when compiler refuses to generate default functions.

Generally C++ compiler generates default functions like Constructor, Copy Constructor, Copy Assignment operator and Destructor if not defined. But there are some cases when compiler also refuses to generate these functions. Consider the below example : name  is a reference and age  is a const T. template<typename T> class PersonObject { public: // this ctor no longer takes a const name, because name // is now a reference-to-non-const string. The char* constructor // is gone, because we must have a string to refer to. NamedObject(std::string& name, const T& age); ... // as above, assume no // operator= is declared private: std::string & name; // this is now a reference const T age ; // this is now const }; Now consider what should happen here: string person1("Tom"); string person2("Mike"); PersonObject<int> p1 (person1, 24); PersonObject<int> p2 (person2, 30); p1 = p2;   // What should happen to the data