Delete references?

I'm using references for a game engine I'm making, and I was wondering if it was possible to use delete on references. Would it just be like this?

1
2
instance& myinst=create_instance(...);
delete myinst;


Or would I have to do this?

1
2
instance& myinst=create_instance(...);
delete &myinst;


Is this even possible?
You don't delete references. What does create_instance do? I'd like to see it full signature and implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//dynamically creates an instance (NOTE: This is the only way to declare an instance)
//create_instance(object type,x,y)
//returns a reference to the instance
template<typename T>
T& create_instance(object& type,int x,int y){
	return *(new T(type,x,y));}

...

instance::instance(object& t,int X,int Y){//base instance class constructor
	type=t;
	startx=X;
	starty=Y;
	x=X;
	y=Y;
	gravity=0;
	gravity_direction=270;
	gravaccel=0;
	speed.value=0;
	speed.refer=this;
	direction.value=0;
	direction.refer=this;
	hspeed.value=0;
	hspeed.refer=this;
	vspeed.value=0;
	vspeed.refer=this;
	if(game.freeids.size()>0){
		id=game.freeids[0];
		game.ids[id]=*this;
		game.freeids.erase(game.freeids.begin());}
	else{
		id=game.ids.size();
		game.ids.push_back(this);}
	sprite_index=0;
	image_index=0;
	image_speed=1;
	visible=true;
	width=t->image->width;
	height=t->image->height;
	if(t->create!=NULL){
		t->create(this);}}


Basically it just creates a new instance. In the constructor, it initializes some variables, adds itself to a list of instances, and calls the create event (if it exists). I needed it to be dynamically allocated because then you could destroy instances whenever... There are probably better ways to do this, though. Originally, create_instance and all related functions dealt with pointers, but I felt more comfortable using "." than "->," so I started remaking the functions so that they handled references.
Last edited on
1
2
3
T& create_instance(object& type,int x,int y){
	return *(new T(type,x,y));
}


You *could* technically delete this elsewhere by deleting the address of the reference, but that is quite a weird thing to do. I would just return a normal object or a pointer.
This was totally, definitely, absolutely not even being close to baring resemblance to the layout of Game Maker instances.

Try making only what you need, really. If you make a platform game, gravity is good to have, but speed is useless for most instances. If you make a topdown game of any sort, gravity is useless, yet speed IS useful. Some instances might not be animated, while still having image_index variables. (Oh, and you forgot image_alpha, _angle, friction and some others (if I recall correctly) :P)

Furthermore, using delete with references can only be used when the reference points to a pointer of some sort. (Which is logical, since a reference is really only a way to point to another object, it's therefore used in the exact same way as the "pointed-to"-object.)
Last edited on
If you want to do this I would recommend returning a pointer, not a reference:
1
2
3
4
template<typename T>
T* create_instance(object& type,int x,int y){
	return new T(type,x,y);
}

That way the caller expects to be the owner and deleter of the returned object. References could cause a lot of confusion.
Yes, I based it off of Game Maker, but not everything is implemented yet. Besides, you've only seen 40 lines of code... My engine already has at least a thousand lines of code, up to 200 lines per file, with 10 files. I'm making this as a general-purpose engine, adding functionality as I need it for use later (or sometimes just when I'm bored and thought of a new function).

I'm not sure how to get image_angle working, so if you have any suggestions, they're welcome. Image_alpha I just haven't gotten around to, yet.
Last edited on
Topic archived. No new replies allowed.