Object Pointer Vector Initialization

I am not very experienced with C ++ yet, so bear with me if this is the main material.

I have code like below. Lis an abstract class (it has a number of pure virtual functions), and A, Band Care regular classes, all derived from L. There can be any number of them, and they are all different.

int main() {
    // ...

    std::vector<L*> ls(3) ; 

    ls[0] = new A ;
    ls[1] = new B ;
    ls[2] = new C ;

    int i ;
    for (i = 0 ; i < ls.size() ; i++) {
        if (ls[i]->h()) {
            // ...
        }
    }

    // ...
}

This works, but there really should be a better way to initialize this vector. Correctly?

The vector should not change after it has been initialized first. I believe that I cannot do this const, because different objects can themselves change inside. I chose a vector over a regular array, because I do not want to manually track its length (which turned out to be error prone).

main , #include. , , " , " = ". A, B C .

, , delete - new, ls delete delete[]. delete ls;, , "type" class std::vector < L *, std:: allocator < L * → , ", ".

?

+3
4

. ?

, , ++ 0x. ? .

, const, .

const, - const.

, ( ).

:

L* ls[] = { new A, new B, new C };
// with <boost/range/size.hpp>
std::size_t num = boost::size(ls);
// without Boost, more error-prone
// std::size_t num = sizeof ls / sizeof ls[0];

, . Boost.Range.

, # .

. , .

, , , new, ls [].

, ls new, . delete , .

STL-, , - Boost.

+4

delete . delete , . - :

for(size_t i = 0; i < ls.size(); i++){
    delete ls[i];
}

. .

void init_vector(std::vector<LS*> & v){
    ls[0] = new A ; 
    ls[1] = new B ;
    ls[2] = new C ;
}
+1

++ 11 , std::array std::vector:

std::array<L *, 3> = {new A(), new B(), new C()};
0

, array vector. array C- , vector. size() ..

, delete, :

#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

boost::array<boost::shared_ptr<L>, 3> ls = { {
    boost::make_shared<A>(),
    boost::make_shared<B>(),
    boost::make_shared<C>(),
} };

array shared_ptr :

#include <array>
#include <memory>

std::array<std::shared_ptr<L>, 3> ls = { {
    std::make_shared<A>(),
    std::make_shared<B>(),
    std::make_shared<C>(),
} };

Please note that the outermost curly braces are not technically necessary, but their exclusion can cause compiler warnings, at least what happens to my compiler.

Ideally, I would like to output the definition and initialization of the vector from mainand preferably to a separate file, which I can then#include

In this case, you need a header file with a declaration and an implementation file with a definition ls:

// file ls.h

#ifndef LS_H
#define LS_H

#include <boost/array.hpp>
#include <boost/shared_ptr.hpp>

extern boost::array<boost::shared_ptr<L>, 3> ls;

#endif

// file ls.cpp

#include "ls.h"
#include <boost/make_shared.hpp>

boost::array<boost::shared_ptr<L>, 3> ls = { {
    boost::make_shared<A>(),
    boost::make_shared<B>(),
    boost::make_shared<C>(),
} };
0
source

All Articles