How do I do this in C++?

This is a discussion on How do I do this in C++? within the C++ Programming forums, part of the General Programming Boards category; Ok, in Blitz Basic, the language I have been using for awhile (I've now decided to move to more "real" ...

How do I do this in C++?

Ok, in Blitz Basic, the language I have been using for awhile (I've now decided to move to more "real" programming) I used these neat things called types all the time. They were very handy for all manner of things, but especially dynamically spawned entities like bullets and such in a game.

Code:

Type monster
Field x#,y#
Field speed#
Field facing#
End Type

Which, of course, would C++ as

Code:

struct monster
{
int x;
int y;
int speed;
int facing;
};

It seems obvious that types are structs, and they behave pretty much the same way. That's all good with me so far, but I do have a problem. In Blitz you could, for example, do this:

which would throw another monster on the pile so to speak. It would have no discernable name, so you would simply use a loop, like this, to process the whole set:

Code:

Function update_monsters()
For m.monster= Each monster
//some code in here would move each monster according
//speed and facing and update their respective coords
//as it cycled through each "monster"
If collide(m\x,m\y) > 0 Then Delete m.monster
//this would delete the current monster if the condition was met
Endif
Next
End function

Now, before you correct me, I am well aware of the syntax differance in functions, as well as the . instead of the \ for values within structs, but I was keeping it in Blitz Basic code for consistancy. Now, is there a c++ equivalent to the generic creation, that is the m.monster = NEW monster and the Delete m.monster and the next looping that sorts through the whole stack of the suckers?

This was probably the coolest little trick in Blitz and it made all kinds of things that would have been horribly twisted very easy to code, by facilitating the dynamic creation and deletion of various entities as needed. Please tell me how I can do something similar in C++. I am using Bloodshed Dev if it matters.

C++ doesn't have the feature that you're talking about (if I am assuming correctly).

It looks like Blitz basic keeps a running list all of all objects of each type. C++ provides no facility like this. You'll have to create a list yourself.

But, if you like this functionality, you can maintain a static list all of entities of a particular object.

Code:

#include <list>
using std::list;
struct Monster
{
// this is a static member. For struct/class members, this means
// that there will only be one of these shared among all
// instances of the class.
static list<Monster*> monsters;
// this is a constructor. It allows you to create a new monster
// with the new keyword:
// Monster* monster = new Monster(x, y, facing, speed);
// It will setup the monster and add it to the static list
Monster(int x, int y, int facing, int speed)
{
this->x = x;
this->y = y;
this->facing = facing;
this->speed = speed;
monsters.push_back(this);
}
// This is a destructor. It is called when you delete the monster.
// It will remove it from the static list.
~Monster()
{
monsters.remove(this);
}
int x;
int y;
int facing;
int speed;
};
// this is necessary to initialize static members
list<Monster*> Monster::monsters;

in C++ the keywords new and delete can be used to create individual objects at run time. These can then be added to any number of containers (of which a list is an example, but there are also arrays, vectors, stacks, trees, queues, maps, sets, etc.) depending on your needs. you can even create an array (which is one of the containters) of objects, each with a given default set of data, at once using the new and delete operators with the [] operator.

new would seem to correlate with New
delete would seem to correlate with Delete

The difference seems to be that new returns an address of the new object (the object has no name, just as in Blitz) and assigns it to a pointer of that object type. So to do what you want in C++ you need to know about structs/classes, pointers, and the new and delete operator (with the [] variants if you want). With those and little knowledge of control loops (which are techniques to go through a sequence of objects perfoming the same set of tasks on each object) and containers, you can do what you want in C++, maybe even with a little more flexibility.