providing classes

M

Matthias Matker

Good morning together,

perhaps you can help me:

I am writing a program which includes some classes, e.g. TUsers, TJobFile.

These classes are written on my own.

In the function "main", I created an instance of TUsers and TJobFile.
These classes are only created once.

TUsers* U = new TUsers;
....

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.

Thank you
 
J

John Harrison

Matthias said:
Good morning together,

perhaps you can help me:

I am writing a program which includes some classes, e.g. TUsers, TJobFile.

These classes are written on my own.

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
 
M

Matthias Kaeppler

Matthias said:
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.

If it must not be created twice, and if it's shared by many classes,
then this screams for making TUsers a singleton.

class TUsers
{
public:
static TUsers& instance();
...
private:
TUsers(); // make ctor private
};

inline TUsers& TUsers::instance()
{
static TUsers tu;
return tu;
}

Now you can acquire a handle to the one and only TUsers object
everywhere you include the TUsers header:

....
TUsers& tu = TUsers::instance();
tu.foobar();
....
 
J

Jim Langston

John Harrison said:
Matthias said:
[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.
 
M

Matthias Matker

Matthias said:
If it must not be created twice, and if it's shared by many classes,
then this screams for making TUsers a singleton.

class TUsers
{
public:
static TUsers& instance();
...
private:
TUsers(); // make ctor private
};

inline TUsers& TUsers::instance()
{
static TUsers tu;
return tu;
}

Now you can acquire a handle to the one and only TUsers object
everywhere you include the TUsers header:

...
TUsers& tu = TUsers::instance();
tu.foobar();

Thank you. I used "singleton" in php5, but I didn't know if that way is
also usable in c++ ;-)

Matze
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top