Skip to main content
deleted 1 character in body
Source Link
463035818_is_not_an_ai
  • 128.6k
  • 11
  • 116
  • 240

It's not possible to derive the static type of the polymorphic object so as to make a setaticstatic declaration of type as in:

my_static_type object; 

There are a couple of things you could do.

One is to put a clone() or create() style function in your polymorphic classes to return correctly typed dynamic objects.

BaseClass* n = root->search_by_name(process_object)->create(); // new object 

The other would be to manually query the possible types, which rather defeats the point of polymorphism:

auto* p = root->search_by_name(process_object); BaseClass* b; if(auto* n = dynamic_cast<Derived*>(p)) b = new Derived; else if(auto* n = dynamic_cast<Derived2*>(p)) b = new Derived2; else throw std::runtime_error("problems problems"); 

NOTE: Pointers used for exposition only, use std::unique_ptr etc.. in real code.

It's not possible to derive the static type of the polymorphic object so as to make a setatic declaration of type as in:

my_static_type object; 

There are a couple of things you could do.

One is to put a clone() or create() style function in your polymorphic classes to return correctly typed dynamic objects.

BaseClass* n = root->search_by_name(process_object)->create(); // new object 

The other would be to manually query the possible types, which rather defeats the point of polymorphism:

auto* p = root->search_by_name(process_object); BaseClass* b; if(auto* n = dynamic_cast<Derived*>(p)) b = new Derived; else if(auto* n = dynamic_cast<Derived2*>(p)) b = new Derived2; else throw std::runtime_error("problems problems"); 

NOTE: Pointers used for exposition only, use std::unique_ptr etc.. in real code.

It's not possible to derive the static type of the polymorphic object so as to make a static declaration of type as in:

my_static_type object; 

There are a couple of things you could do.

One is to put a clone() or create() style function in your polymorphic classes to return correctly typed dynamic objects.

BaseClass* n = root->search_by_name(process_object)->create(); // new object 

The other would be to manually query the possible types, which rather defeats the point of polymorphism:

auto* p = root->search_by_name(process_object); BaseClass* b; if(auto* n = dynamic_cast<Derived*>(p)) b = new Derived; else if(auto* n = dynamic_cast<Derived2*>(p)) b = new Derived2; else throw std::runtime_error("problems problems"); 

NOTE: Pointers used for exposition only, use std::unique_ptr etc.. in real code.

Source Link
Galik
  • 49k
  • 5
  • 85
  • 126

It's not possible to derive the static type of the polymorphic object so as to make a setatic declaration of type as in:

my_static_type object; 

There are a couple of things you could do.

One is to put a clone() or create() style function in your polymorphic classes to return correctly typed dynamic objects.

BaseClass* n = root->search_by_name(process_object)->create(); // new object 

The other would be to manually query the possible types, which rather defeats the point of polymorphism:

auto* p = root->search_by_name(process_object); BaseClass* b; if(auto* n = dynamic_cast<Derived*>(p)) b = new Derived; else if(auto* n = dynamic_cast<Derived2*>(p)) b = new Derived2; else throw std::runtime_error("problems problems"); 

NOTE: Pointers used for exposition only, use std::unique_ptr etc.. in real code.