I am creating a text adventure where items have a virtual ITM class and sub-specific classes, ie.
[code]door Door;
ITM* p0001 = &Door;
[\code]
I need the items to be versatile so they are loaded when the room gets loaded, thus have the class creation in a function. Is it possible to dynamically create classes and prevent destruction of such a class upon leaving scope?
ie.
[code]
void makeItms(int i){
switch(i){
case 1:
door woodenDoor;
lantern Lantern;
break;
case 2:
door steelDoor;
rug persianRug;
break;
}
}
[\code]
be able to make the items in makeItms depending on room, then alter the classes outside of the function.
The pointer p0001 will go out of scope but the memory allocated by "new door" will not. As long as you store a copy of the pointer elsewhere, you can access the memory out of scope.
as long as the variable you are using is the same type as the constructor you are calling you can use any constructor, and yes that would be the correct syntax for creating a new door via dynamic allocation
that way you could call it via: dynamicConstructor<door>(); or dynamicConstructor<rug>(); and it would work the same
you could even do dynamicConstructor<int>(); or dynamicConstructor<std::string>(); or dynamicConstructor<float>(); and it would still work just the same
i was wrong, the operator wouldnt work, but everything else does
ie:
1 2 3 4 5 6 7 8 9 10 11 12
#include "Dynamic Allocators.h"
usingnamespace da;
int main ()
{
int &x = dynamic<int>(); //creates a new dynamic variable
x = 10; //works
int *y = dynamic_array<int, 20>(); //creates a new dynamic array of size 20
*(y + 10) = 5; //same as y[10] = 5;
return 0;
}
works with this header included into your file
(Create a cpp in your project then go to Project->[Project Name] properties->C/C++->General
and include the directory you save it in...)
its a replacement for the "factory" function that clanmjc made, its not all that different than using new but im considering adding a class that stores the created pointers to prevent leaks :/