码迷,mamicode.com
首页 > 其他好文 > 详细

从vector继承的类

时间:2017-01-17 18:39:06      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:blog   get   tin   back   ora   range   null   public   startup   

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 
 8 class AVP
 9 {
10 private:
11     char *name;
12 public:
13     AVP()
14     {
15         name = NULL;
16     }
17     
18     AVP(const char* _name)
19     {
20         name = new char[strlen(_name) + 1];
21         strcpy(name, _name);
22     }
23     char* getName()
24     {
25         return name;
26     }
27 };
28 
29 class Read: public vector<AVP*>
30 {
31 public:
32     bool startup()
33     {
34         AVP* a1 = new AVP("apple");        
35         push_back(a1);
36         a1 = new AVP("orange");        
37         push_back(a1);
38         a1 = new AVP("pear");        
39         push_back(a1);
40         
41     }
42     static Read* getInstance()
43     {
44         if(instance == NULL)
45             instance = new Read();
46         return instance;
47     }
48     void print()
49     {
50         iterator it = begin();
51         for(; it != end(); ++it)
52         {
53             cout<<(*it)->getName()<<endl;
54         }
55         
56     }
57 protected:
58     Read()
59     {
60     }
61 private:
62     static Read* instance;
63 
64 };
65 Read* Read::instance = NULL;
66 int main()
67 {
68     Read::getInstance()->startup();
69     Read::getInstance()->print();
70     
71     
72     return 0;
73 }

如果不理解Read类从public vecotr<AVP*>继承的话,可以这样,vector<AVP*> _avp; 

Read类从_avp继承,这样就很好理解了,直接看代码

从vector继承的类

标签:blog   get   tin   back   ora   range   null   public   startup   

原文地址:http://www.cnblogs.com/chuanyang/p/6294100.html

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