+ 1
I need help
im working on a game and i want to do something like this Block blocks[10000]; int main(){ blocks[0]=new StoneBlock(/*paramrters*/) return 0; } and the Block class is parent class of StoneBlock
6 Answers
+ 2
std::shared_ptr<Block> blocks[10000];
blocks[0] = std::make_shared<StoneBlock>(ARGUMENTS)
+ 1
or the other option is to tell me how can i declare an array of typeless elements which i can give type on while the program is running
+ 1
but i dont know if is it possible
+ 1
can you explain this to me
+ 1
You can use pointer to base class (Blocks*) to point inherited class (StoneBlock*).
Blocks* blocks [10000] = {};
Unfortunately raw pointers isn't safe enough. You can get memory leak if miss deleting the objects.
Shared pointer is a wrapper around the raw pointer, that will delete object for you at it's destructor in case if there are no other shared pointer that points your object. You also can clear it manually by reset() function.
+ 1
thanks this helps a lot :)