The first piece of code does not compile, whereas the second one does. Why?
The code is almost the same indeed. I would be grateful to have some help with this question.
The code below does not compile. You could check it on http://cpp.sh/8j53y.
dynamic_cast
#include <iostream>
#include <memory>
using namespace std;
class Derived;
class Base {
public:
static unique_ptr<Base>& get_instance()
{
pBase = make_unique<Derived>();
return pBase;
}
private:
static unique_ptr<Base> pBase;
};
class Derived: public Base { };
std::unique_ptr<Base> Base::pBase = nullptr;
int main () {
auto& instance = Base::get_instance();
return 0;
}
The code below does compile.
#include <iostream>
#include <memory>
using namespace std;
class Derived;
class Base {
public:
static unique_ptr<Base>& get_instance();
private:
static unique_ptr<Base> pBase;
};
class Derived: public Base { };
std::unique_ptr<Base> Base::pBase = nullptr;
unique_ptr<Base>& Base::get_instance()
{
pBase = make_unique<Derived>();
return pBase;
}
int main () {
auto& instance = Base::get_instance();
return 0;
}