Advertisement

Classes - making objects 'global'

Started by June 22, 2001 01:06 PM
18 comments, last by Rottbott 23 years, 5 months ago
Hi, I''m very new to C++, coming from a BASIC background. I''m interested in using a class to hold vertex data for an OpenGL program. However, it won''t work unless it is possible to define objects in one function and use them elsewhere. Can this be done? My compiler tells me not. Here''s what I''m trying to do (simplififed): class Person { public: char name; int age; }; void Init(void) { Person Fred; Fred.age = 20; } int Main(void) { int a = Fred.age } This gives me a "Fred not defined" error in Main(). How can I declare the Fred object in Init() or any other function but still use him in Main? Rottbott
class Person
{
public:
char name;
int age;
};


Fred* Init(void)
{
Person* Fred = new Person();
Fred->age = 20;

return Fred;
}

int Main(void)
{
Person* Fred = Init();
int a = Fred->age;
}
Advertisement
Hehe this message board doesnt have C++ code check;

Its supposed to be:

Person* Init() {
Person* Fred = new Person();
Fred->age = 20;
return Fred;
}

void Main() {
Person* Fred = Init();
int a = Fred->age;

delete Fred;
}
1. Your Init function isnt really necessary, just use a constructor...

    class Person{  public:  Person(int personAge = 20) // Constructor. Age is set to 20 unless otherwise specified.  {    age = personAge;  }  char name;  int age;};int Main(void){  Person Fred; // Use default age of 20  Person Boris(25) // specify age 25  int a = Fred.age;  int b = Boris.age;}    


2. If you really want global objects, declare them outside of a function like this...
  // in header file....extern Person Fred; // fred is available to any function provided this header is includedstatic Person Nigel; // nigel is only available to the functions defined in this header.// note that these declarations dont instantiate // Fred and Nigel, they just make them visible to other files// in cpp filePerson Fred(23);   // Instantiate FredPerson Nigel(11);  // Instantiate Nigel  



Edited by - Sandman on June 22, 2001 2:48:49 PM
Thanks for the replies, but I think I made myself unclear. I know how to use constructors to specify default values etc, but that''s not really what I''m after. I want to be able to create Fred or Boris anywhere in my code, _without warning_, and be able to access him from elsewhere. I tried using ''extern'' but it didn''t work...


If it helps, what I''m actually doing is trying to use a class to hold vertex data like so:


class Triangle {
public:
float VertexX[3];
float VertexY[3];
float VertexZ[3];
};


I then want to create and destroy Triangles at different points in my code, and process each Triangle for rendering in a Display() function. However, when I create a new Triangle they end up being local to the function that created them.


Rottbott
If i indeed correct what you need to do is:

FILE: person.h
  class Person{public: char name; int age;};// global objectsextern Person Fred;// global functions prototypevoid Init (void);  


FILE: person.cpp
  #include "person.h"Person Fred;  // important (only write this in one source file)void Init (void){ Fred.age = 20;}  


FILE: other.cpp
  #include "person.h"int main (int argc, char **argv){ int a = Fred.age;}  
Advertisement
You can''t really declare global data from within a function. What''s usually done is to create the object with ''new'' and maintain a pointer to it. This object will remain in memory until you explicitly destroy it with ''delete''.

for example:

Person* CreatePerson()
{
jpastor, thanks for the explanation but the thing is I don''t know how many ''people'' I want in my program. I''ll be reading data from a file which will tell me which ''people'' to make.

Anon, the pointer thing - I''ll try that.

It may be I''m going about this completely the wrong way.

Thanks for all your replies.


Rottbott
er... ok, that wasn''t exactly the most gracefull first post ever :/ (I''m the previous annonymous poster)

Is there a reason tab doesn''t work in text editing on a forum devoted to programming? Or am I just being silly?

Back to the topic:

Create the objects with ''new'' and maintain a pointer to it. You might want to keep the pointers in a global repository like a linked list or something to help keep track of them.

If you don''t keep track of your pointers, you''ll end up causing memory leaks, so remember to explicitly ''destroy'' all the objects you create with ''new'' when you''re done with them.
I''ve decided to store pointers in an array of ''unsigned int'' (hopet this is allowed). Now how do I access the object again FROM a pointer? (Sorry if this has already been covered, I''ve not used pointers before today!).


Rottbott

This topic is closed to new replies.

Advertisement