Prevent destruction when leaving scope

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.
Last edited on
An alternative
container.insert( new door ); where container contains (smart) ITM pointers.
closed account (S6k9GNh0)
It's called heap. ITM* p0001 = new door;

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.
Last edited on
can you call
ITM* p0001 = new door(/*constructor vars*/);, or does that only work for actual classes? this is very relevant ...
Last edited on
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
closed account (S6k9GNh0)
http://codepad.org/B6khBXLc

C++ allows function-like initialization for basic types. For instance: char* test("lol"); is allowed.

http://codepad.org/h6wHmXJ3
You can use a factory for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ITM * makeItms(int i)
{
   switch(i){
   case 1:
      return new door;
   case 2:
      return new rug;
   default:
      return NULL;
   }
}

//...somewhere else

ITM * pItem = makeItms(2);
it would probably be more efficient to use a template, ie:
1
2
template<class _TYPE>
_TYPE * dynamicConstructor () { return new _TYPE; }


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

and you could do something like this for arrays:
1
2
3
4
5
6
7
8
template<class _TYPE, int _ARRAY_SIZE>
_TYPE* dynamicArray () { return new _TYPE [_ARRAY_SIZE]; }

template<class _TYPE>
_TYPE& operator [] (_TYPE * variable, unsigned int index)
{
    return *(variable + index);
}


which would work via:
1
2
int * x = dynamicArray<int, 20>;
x[10] = 0;


see? just like normal :)

where it would create an array that you could use the exact same as a normal array... if im correct, all speculation :)
Last edited on
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"

using namespace 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...)

here's the header: https://docs.google.com/open?id=0B94sQ7a2ETgiS2xhSEh0SUFSMy02V25lbUdBLUprdw
Last edited on
¿how is that different than just using new directly?
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 :/
closed account (S6k9GNh0)
int *y = dynamic_array<int, 20>();

I don't get how this is supposed to be a replacement to the factory concept. Rather, it seems utterly useless to be honest.

The only place I can think of where that would be useful is maybe wrapping the allocation process for garbage collection or something.
Last edited on
Topic archived. No new replies allowed.