Is it possible to create a vector of pointers?

后端 未结 6 2290
情歌与酒
情歌与酒 2020-12-15 01:27

Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin()

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 01:40

    Yes, sure.

    // TestCPP.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include 
    #include 
    
    
    using namespace std;
    
    class c
    {
    public:
        void virtual func() = 0;
    };
    
    class sc:public c
    {
    public:
        void func(){cout<<"using func";}
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        sc cobj;
    
        vector cvect;
        cvect.push_back(&cobj);
        vector::iterator citer;
    
        for(citer=cvect.begin();citerfunc();
        }
    
        return 0;
    }
    

    Please note the declaration of vector cvect and the use of cvect.push_back(&cobj).

    From the code provided, you are using iterator in a wrong way. To access the member an iterator is pointing to you must use *citer instead of citer alone.

提交回复
热议问题