Static member function and run time polymorphism [closed]

  c++, c++11, static-members
class Base
{
    private:
    Base() = default;
    static Base *b;
    public:
    static Base* get();
};
class Derived: public Base
{

};
Base* Base::b=nullptr;
Base* Base::get(){Base* b = new Derived();return b;}
void main()
{
     Base* b = Base::get();  
}

I get a compile time error:

main.cpp: In static member function 'static Base* Base::get()':
main.cpp:14:41: error: use of deleted function 'Derived::Derived()'
   14 | Base* Base::get(){Base* b = new Derived();return b;}
      |                                         ^
main.cpp:9:7: note: 'Derived::Derived()' is implicitly deleted because the default definition would be ill-formed:
    9 | class Derived: public Base
      |       ^~~~~~~
main.cpp:9:7: error: 'constexpr Base::Base()' is private within this context
main.cpp:4:5: note: declared private here
    4 |     Base() = default;
      |     ^~~~

Live Example

In Base::get function, if i do Base* b = new Base(); or remove the private Base() constructor and make it public, i dont get any error.

Source: Windows Questions C++

LEAVE A COMMENT