zoukankan      html  css  js  c++  java
  • 从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继承,这样就很好理解了,直接看代码

  • 相关阅读:
    mysql面试题
    Excel下载打不开
    Linux安装jdk1.8和配置环境变量
    Linux压缩、解压文件
    Linux常用命令1
    VMware下载安装及CentOS7下载安装
    ueditor的简单配置和使用
    linux的tomcat服务器上部署项目的方法
    TortoiseSVN客户端的使用说明
    CentOS 6.5系统上安装SVN服务器
  • 原文地址:https://www.cnblogs.com/chuanyang/p/6294100.html
Copyright © 2011-2022 走看看