I'm trying to create a template blueprint type class and trying to figure out the best way to handle instancing a new class (From a templated) class. Essentially use a class to dynamically instance objects from. This may seem stupid but I need the class (Shapes) to hold their own object of type 'types' Something like this:
class Shape{
vector<Types> shapes;
MyShape Create(){
foreach(auto shape in shapes){
//Create a new instance each shape here
//Along the lines of?
//shape s();
}
}
}
class Circle : Shape{
Circle(){
this->shapes.push_back("A CLASS OF SHAPE");
}
}
Circle cir();
//I'd change the return type, to either a vector or just push each instance back to a vector but just using the below as an example.
MyShape myShape = cir->Create();
I'm coming from C# so please tell me if this is bad practice.
What are you trying to accomplish? You know that in C# any complex object is accessed via a pointer. In C++ you can use [smart] pointer to gain the same effect.
In C++ you can store the entire object in a container. Whether that makes sense depends on the use of this object.
This won't make much sense till I show an example, I want the base class to handle the instancing / logic of the inherited class because I don't want to manually write hundreds of shapes:
class SuperBar{
//...
}
class Bar : public SuperBar{
//...
}
class Foo : ManufactureSuperBar{
vector<map<int, Bar>> requiredNumberToInstanceBar;
Foo(){
Bar bar();
requiredNumberToInstanceBar.push_back(std::make_pair(12, bar);
//OR pass in something like this
ManufactureSuperBar::Create(12, bar);
}
}
class ManufactureSuperBar{
Create(int amount, SuperBar superBar){
if(amount > 10)
...
}
}
I want the base class to handle the instancing / logic of the inherited class because I don't want to manually write hundreds of shapes.
IOW, you want a factory function or method:
1 2 3 4 5 6 7 8 9 10 11 12 13
class Bar { virtual ~Bar() = default; }; // base class
struct D0: Bar {}; // derived type 0
struct D1: Bar {}; // derived type 1
// ...
using A = some_type;
// place this depending on your needs
// e.g., if the base-class constructor is protected
std::unique_ptr<Bar> make_bar(A selector) {
if (selector == D0_selected) return std::make_unique<D0>();
else (selector == D1_selected) return std::make_unique<D1>();
elsereturnnullptr;
}
I don't want to manually write hundreds of shapes:
Of course not. It seems to me that you want a container of all derived shapes in the shape class itself. I don't think it makes too much sense.
Again: C# and C++ are so closely related that you can achieve absolutely the same in both languages just with some minor syntax differences. I do actually work with both languages. In C++ you might find more elegant solution because it is less restricted.