+ 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

5th Sep 2018, 9:57 AM
darkorbit17493
6 Answers
+ 2
std::shared_ptr<Block> blocks[10000]; blocks[0] = std::make_shared<StoneBlock>(ARGUMENTS)
5th Sep 2018, 11:12 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 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
5th Sep 2018, 10:01 AM
darkorbit17493
+ 1
but i dont know if is it possible
5th Sep 2018, 10:01 AM
darkorbit17493
+ 1
can you explain this to me
5th Sep 2018, 11:23 AM
darkorbit17493
+ 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.
5th Sep 2018, 11:45 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 1
thanks this helps a lot :)
5th Sep 2018, 2:15 PM
darkorbit17493