Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Rottbott

Classes - making objects 'global'

This topic is 6235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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 included

static 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 file


Person Fred(23); // Instantiate Fred

Person Nigel(11); // Instantiate Nigel





Edited by - Sandman on June 22, 2001 2:48:49 PM

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
If i indeed correct what you need to do is:

FILE: person.h
  
class Person
{
public:
char name;
int age;
};

// global objects

extern Person Fred;

// global functions prototype

void 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;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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()
{

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!