"John Harrison" <jo*************@hotmail.com> wrote in message
news:3a*****************@newsfe2-win.ntli.net...
Matthias Matker wrote: [snip]
In the function "main", I created an instance of TUsers and TJobFile.
These classes are only created once.
TUsers* U = new TUsers;
Why use new?
TUsers U;
is better for this because you don't have to remember to use delete.
...
By the way: the class above manages the users accounts.
But how can this TUsers "U" be provided in other parts of the program?
For example in a new class called TAccess which needes the data of U?
U must be global or something like this, I may not be created twice.
Don't use global variables that's what amateurs (and newbies) do.
Pass a pointer to U to the constructor of TAccess, that is one way
Something like this
class TAccess
{
public:
TAccess(TUsers* users) { my_users = users; }
private:
TUsers* my_users;
};
int main()
{
TUsers U;
TAccess A(&U);
...
}
No TAccess has it's own memober variable which is a pointer to the users.
Thank you
no problem,
john
Just make sure you do NOT delete my_users in TAccess. Since TAccess didn't
create the object, it shoulnd't delete it.
In the sample given, since main created it, it would delete it, but since it
was now created using new nothing has to delete it (it will get deleted
automatically when the main function exits).
If main had used TUsers* U = new TUsers; then of course main would call
delete U; at the end.