473,473 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

providing classes

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
Sep 11 '05 #1
4 1147
Matthias Matker wrote:
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
Sep 11 '05 #2
Matthias Matker wrote:
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();
....

--
Matthias Kaeppler
Sep 11 '05 #3
"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.
Sep 11 '05 #4
Matthias Kaeppler schrieb:
Matthias Matker wrote:
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();


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

Matze
Sep 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jason Mobarak | last post by:
Greetings! Say that it's desirable to provide backwards compatibility for methods of an object, consider the case where... class Foo: def bar (self, a, b): pass ....is a defined class...
3
by: Daniel Schüle | last post by:
Hello Ng, I was playing around with pymedia module and I succeeded when I used complementation instead of inheritance .. but then I was forced to wrap simple methods of sound.Output like...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
0
by: Lee | last post by:
Once the business logic has been written and the basic elements are built into a page, what is the recommended way to provide the site innards to graphics design people without providing source...
0
by: Adam Sandler | last post by:
Hello, Having an issue with my ASP.Net page. I use some COTS, which for all intents and purposes, simply makes a jpeg file and physically places it in a directory on the web server. The...
5
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)?...
0
by: watches0898 | last post by:
Edwards Garments Company is one popular industry specific designer specializing in work wear that ranges from chef coats to chef hats to separates to housecleaning uniforms. Edwards Garments...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.