Jump to content
  • Advertisement

Archived

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

Rottbott

Classes - making objects 'global'

This topic is 6576 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

First off, you can declare an array of object pointers just like any other data type. Therefore you don''t need to make them unsigned int.

class Person()
{
stuff
more stuff
};

Person* PersonArray[20];

void main()
{
...
}

When referencing an object from a pointer just use ''->'' instead of ''.''.

pBob->name = "Bob Onanob\n"

instead of

Neil.name = "Neil N. Bob\n"


Id reccomend using a linked list instead of an array. But since you''re new to pointers go ahead, just be carefull not to make more objects than the array can hold.

Share this post


Link to post
Share on other sites
Advertisement
Ah, it''s beginning to make some sense now.

Linked lists - I will one day, but I''d like to tackle one new thing at a time as I learn .

Couple of things to clear up:

1) " pBob->name = "Bob Onanob\n" "

Shouldn''t that be *pBob rather than just pBob?


2) How do I assign the address to the pointer? I thought it was this but it doesn''t work:

PersonArray[1] = &Bob;


Rottbott

Share this post


Link to post
Share on other sites
Wait, I realised what I was doing for number 2. I was doing *PersonArray[1] instead of just PersonArray[1]

Now I just have to work out how to access Bob from elsewhere via the pointer which I have (hopefully) stored.

Another worry, will I have to do some clearing up to prevent memory leaks doing this? If so, how?

Thanks for all the help so far, you guys are great :D


Rottbott

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Rottbott
1) " pBob->name = "Bob Onanob\n" "
Shouldn''t that be *pBob rather than just pBob?


No. The -> resolves the indirection for you.

quote:

2) How do I assign the address to the pointer? I thought it was this but it doesn''t work:

PersonArray[1] = &Bob;
Rottbott


Assuming Bob is not a pointer (Declared: "Person Bob"), that should work. If bob is declares as a pointer ("Person *Bob") then the ''&'' operator is not used.

Here''s a quick example:


#include
#include
using namespace std;

class Person
{
public:
string Name;
};

Person* PersonArray[10];

void main()
{
Person* pBob = new Person;

pBob->Name = "Bob Onanob";


PersonArray[1] = pBob;

cout << "Name: " << PersonArray[1]->Name << endl;
}

/*
void main()
{
PersonArray[1] = new Person;
PersonArray[1]->Name = "Bob Onanob";

cout << "Name: " << PersonArray[1]->Name << endl;
}
*/


Either of these will work. The second main() function does the same thing just skips the ''pBob'' middleman.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To access Bob just do

Personage = Bob->age

The "->" takes care of the *Bob part (e.g. think of Bob->age as *Bob.age, the last one isn''t legitamite code).

As for memory leaks remember the rule that anything you ''new'' you must ''delete''. So somewhere in the program have

delte Bob, or

for (int i=0; i delte PeopleArray;

I don''t know what you''re working under (MFC, Windows API etc...) but if you''re using Windows API you can just delete functions in the WM_DESTROY message.

Hope my code doesn''t have any mistakes!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To access Bob just do

Personage = Bob->age

The "->" takes care of the *Bob part (e.g. think of Bob->age as *Bob.age, the last one isn''t legitamite code).

As for memory leaks remember the rule that anything you ''new'' you must ''delete''. So somewhere in the program have

delte Bob, or

for (int i=0; i delte PeopleArray;

I don''t know what you''re working under (MFC, Windows API etc...) but if you''re using Windows API you can just delete functions in the WM_DESTROY message.

Hope my code doesn''t have any mistakes! BTW what project are you doing?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To access Bob just do

Personage = Bob->age

The "->" takes care of the *Bob part (e.g. think of Bob->age as *Bob.age, the last one isn''t legitamite code).

As for memory leaks remember the rule that anything you ''new'' you must ''delete''. So somewhere in the program have

delte Bob, or

for (int i=0; i delte PeopleArray;

I don''t know what you''re working under (MFC, Windows API etc...) but if you''re using Windows API you can just delete functions in the WM_DESTROY message.

Hope my code doesn''t have any mistakes! BTW what project is this for?

Share this post


Link to post
Share on other sites
Hmmm...anon again.

Right, using that code the assigning part works, HOWEVER, when I try to use the pointer array (which I defined when I defined the class at the top of my code) in another function, it says it hasn't been created in that function. Any ideas?

EDIT - Now I feel stupid. I mispelled it. Sorry to bother you there. Well, now my code compiles. Off to run it. By the way, what must I do in the way of clearing up after using it like this?

Edited by - Rottbott on June 22, 2001 5:54:41 PM

Share this post


Link to post
Share on other sites
All right! All appears to work beautifully!

Thanks very much to everyone who replied, my C++ knowledge has been extended plenty today. Now I''d better go, this thread is starting to account for a sizeable fraction of the total posts on this board

*Signs out*

Share this post


Link to post
Share on other sites
quote:
Original post by Rottbott
Hmmm...anon again.

Right, using that code the assigning part works, HOWEVER, when I try to use the pointer array (which I defined when I defined the class at the top of my code) in another function, it says it hasn''t been created in that function. Any ideas?


Rottbott




#include "string"
#include "iostream"
using namespace std;

class Person
{
public:
string Name;
};

Person* PersonArray[10];

void SetName( void );


void main()
{
PersonArray[1] = new Person;

SetName();

cout << "Name: " << PersonArray[1]->Name << endl;
}

void SetName( void )
{
PersonArray[1]->Name = "Bob Onanob";
}



Is this what you mean? Is the function in a different source file? If so you need to declare the array as "extern Person* PersonArray[10]" in the other file.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!