从vector继承的类
Posted 川洋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从vector继承的类相关的知识,希望对你有一定的参考价值。
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继承的类的主要内容,如果未能解决你的问题,请参考以下文章
深入解析 Java集合类ArrayList与Vector的区别
深入解析 Java集合类ArrayList与Vector的区别(还没看)