C++ syntax

Started by
4 comments, last by joeG 24 years, 6 months ago
You've got the declaration wrong. To declare a vector of type "object" use:


vector objects;


As for garbage collection/mem deallocation, the vector object handles that automatically in its destructor..

Advertisement
First, you would get a faster response if asked this quetion in a more general programming forum. Enough peaching, on to the answer.

The STL is the "Standard TEMPLATE Library" so you need to use C++ TEMPLATE syntax to instantiate it. In the case of the STL containers, to declare a container write something like:
containerClass<dataType> variable name;
such as:
list<string> students;
vecotr<int> grades;

for your specific case here's come help:

// object.h
class Object
{
static vector<WHAT_TYPE_ARE_THE_OBJECTS> instantiatedObjects;
...
public:
...
};

// object.cpp
vector<WHAT_TYPE_ARE_THE_OBJECTS> Object::instantiatedObjects;

I suspect that you are meaning to replace WHAT_TYPE_ARE_THE_OBJECTS with Object* but I wasn't sure.

Also, realize that if you are going to do this, you are quite likely going to want to add some class level functions to instantiate objects, using one of the creation patterns described in the book "Design Patterns" (by Gamma, Helm, Johnson, Vlissides).

[This message has been edited by Myopic Rhino (edited October 19, 1999).]

That didn't come out right, it should say:

vector&lttype> instanstiatedClasses;

------------------
LoungePig
OpenGUI

I've done all that, although I don't know how to display it within HTML code (because of the greater-than less-than signs that HTML uses).

[This message has been edited by joeG (edited October 19, 1999).]

joeG
Situation: Its very essential that I have an extensible array as a static member of one of my classes. I've chosen stl's vector because I don't know where else to find one and I don't want to make it myself.

Problem: the program is not able to exit without crashing. I'm very sure that it occurs after the last bit of source code.

Here's the code inside the class declaration:
class Object
{
...
static vector instantiatedObjects;
...
};
Here's the code outside the class declaration:
vector Object::instantiatedObjects =
vector();
That'all the relative stuff to my problem.
Any suggestions?

joeG
STL vectors are only as good as the objects you use them with... specifically, if the operator= and copy constructor you've written are buggy, the vector is going to be buggy. (If you haven't written these, C++ will do a bitcopy of your objects, which may also be the source of your problem).

See if you can duplicate the crash with a simple class (i.e., one member variable). If you can't, keep adding stuff to the simple class until it either looks like your original class (congrats!) or the vector blows up.

Mason McCuskey
Spin Studios
www.spin-studios.com

Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!

This topic is closed to new replies.

Advertisement