473,587 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ 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 1154
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::instanc e()
{
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::instanc e();
tu.foobar();
....

--
Matthias Kaeppler
Sep 11 '05 #3
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:3a******** *********@newsf e2-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::instanc e()
{
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::instanc e();
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
1778
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 that can be serialized and later be deserialized.
3
1406
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 pause/unpause/stop. It works, but seems to me unnecessary .. and I would like to grasp why the code below doesn't work
2
9488
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 to do that? Thanks. Tsung-Yu
18
2033
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. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is...
0
967
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 code too? Thanks for advice. -- Warm Regards, Lee
0
1131
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 page load codebehind, makes a connection to the jpeg creation service and gets the path of the newly created image.
5
1636
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.)? Could a Python program itself create a sub-interpreter, and work with it with all the privileges and capabilities that an actual C program would have? ...
0
1309
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 Company has a reputation for providing a high quality, affordable solution to the professional man's or woman's wardrobe. Often, white is the color given...
36
2014
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 there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.