История изменений
Исправление vvviperrr, (текущая версия) :
#include <iostream>
#include <memory>
#include <array>
using std::cout;
using std::endl;
class A {
public:
virtual ~A() {}
virtual void foo() = 0;
};
class B : public A {
public:
virtual void foo() override {
cout << "B::foo" << endl;
}
};
class C : public A {
public:
virtual void foo() override {
cout << "C::foo" << endl;
}
};
class D : public A {
public:
virtual void foo() override {
cout << "D::foo" << endl;
}
};
int main(void) {
std::array<std::unique_ptr<A>, 3> arr = {
std::unique_ptr<A>(new B),
std::unique_ptr<A>(new C),
std::unique_ptr<A>(new D),
};
for (auto &i : arr) {
i->foo();
}
}
Исходная версия vvviperrr, :
#include <iostream>
#include <memory>
#include <array>
using std::cout;
using std::endl;
class A {
public:
virtual ~A() {}
virtual void foo() = 0;
};
class B : public A {
public:
virtual void foo() override {
cout << "B::foo" << endl;
}
};
class C : public A {
public:
virtual void foo() override {
cout << "C::foo" << endl;
}
};
class D : public A {
public:
virtual void foo() override {
cout << "D::foo" << endl;
}
};
int main(void) {
std::array<std::unique_ptr<A>, 3> arr = {
std::unique_ptr<A>(new B),
std::unique_ptr<A>(new C),
std::unique_ptr<A>(new D),
};
for (auto &i : arr) {
i->foo();
}
}