I have two classes, Base
which contains virtual method and D
which contains overload that method. I want to create a variable with Base
type, than pass there class D
which inherits class Base
. It's my implementation:
#include <iostream>
#include <vector>
#include <memory>
#include <cstdio>
#include <fstream>
#include <cassert>
#include <functional>
class Base {
public:
virtual void bar() { std::cout << "B::bar\n"; }
//virtual ~Base() = default;
};
typedef Base* Maker();
Maker* arr[10];
class D : Base
{
public:
D() { std::cout << "D::D\n"; }
~D() { std::cout << "D::~D\n"; }
void bar() override { std::cout << "D::bar\n"; }
};
template <class T>
Base* make(){
return new T;
}
int main()
{
std::unique_ptr<Base> p1(new D);
p1->bar();
//arr[0] = make<D>();
return 0;
}
Btw, it's working with structs, but when I try to implement that though classes I'll get error.