码迷,mamicode.com
首页 > 编程语言 > 详细

c++ constructor, copy constructor, operator =

时间:2018-08-18 19:48:19      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:store   delete   operator   out   some   enter   turn   integer   cin   

// 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;
}

 

c++ constructor, copy constructor, operator =

标签:store   delete   operator   out   some   enter   turn   integer   cin   

原文地址:https://www.cnblogs.com/youge-OneSQL/p/9498176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!