Classes - making objects 'global'

Started by
18 comments, last by Rottbott 22 years, 10 months ago
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.
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
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
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.
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!
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?
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?
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
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*
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.

This topic is closed to new replies.

Advertisement