I have the following classes in a header file designed to create runtime polymorphism for a set of instructions that describe system states in an embedded environment:
class foo
{
public:
virtual void bar(void) = 0;
virtual ~foo() {}
};
class derived1 : public foo
{
private:
int data1;
public:
void bar(void)
{
//implementation 1
};
derived1(int d) : data1(d) {}
~derived1() {}
};
class derived2 : public foo
{
private:
int data2;
public:
void bar(void)
{
//implementation 2
}
derived2(int d) : data2(d) {}
~derived2() {}
};
What is the best way to initialize a vector of type const vector<foo*>
? I am currently doing the following:
const std::vector<foo*> v =
{
new derived1(a),
new derived1(b),
new derived2(c),
new derived2(d)
};
Given the const
qualifier, is this memory still dynamically allocated? It is stored in an anonymous namespace for use by several state classes. Because many state classes share the same elements I wanted to define the vector for all of them to chose from to save code space. Is a vector best suited for this behavior?
Ps, I do not need to worry about calling delete
on any of the pointers since the application requires this information for all runtime.
Aucun commentaire:
Enregistrer un commentaire