c++ constructor, copy constructor, operator =
// list::push_back #include <iostream> #include <list> class element{ private: int number; public: element(int number){ this->number = number; std::cout << "constructor used" << std::endl; } element(const element&){ std::cout << "copy constructor used" << std::endl; } element& operator=(const element& element_instance){ std::cout << element_instance.number << "operator=" << std::endl; this->number = element_instance.number; return *this; } int get_number(){ return this->number; } ~element(){ std::cout << this->number << std::endl; this->number = 0; std::cout << "destructor used" << std::endl; } }; int main () { std::list<element> mylist; int myint; std::cout << "Please enter some integers (enter 0 to end):\n"; do { std::cin >> myint; element* element_instance = new element(myint); mylist.push_back (*element_instance); delete element_instance; } while (myint); //std::cout << "mylist stores " << mylist.size() << " numbers.\n"; std::cout << "first number:" << (mylist.front().get_number()) << std::endl; return 0; }
MySQL限时解答,24小时内友哥专业解答
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指
http://www.yougemysqldba.com
如有进一步需要请联系微信onesoft007
微博账号@友哥一指