473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to define a class that's accessible from anywhere?

Hi!

I need a class to store the users data (ID, name etc.) that is accessible
from anywhere.

At application startup the class gets filled with its data about the user.
But how can i access this data from all other classes?

Normally to get access, i would have to say something like UserData *UD =
new UserData; But this way a new class gets instantieted. What can i do to
access the data from anywhere?

Thanks in advance

Sven
Jul 22 '05 #1
6 2674
"Gunnar Beushausen" <al**************@t-online.de> a écrit dans le message
de news: cq*************@news.t-online.com...
Hi!

I need a class to store the users data (ID, name etc.) that is accessible
from anywhere.

At application startup the class gets filled with its data about the user.
But how can i access this data from all other classes?

Normally to get access, i would have to say something like UserData *UD =
new UserData; But this way a new class gets instantieted. What can i do to
access the data from anywhere?


First, learn to distinguish between class and objects.

What you need is a global object and that is usually not recommended. There
are other ways, but your design may force you into that.

You have several choices. One would be to declare the object (using extern)
in a header and include the header wherever you need the object. The object
would be defined in one implementation file.

// myclass.h

class MyClass
{
public:
void f();
};

extern MyClass o;

// myclass.cpp

MyClass o;
// test.cpp
# include "myclass.h"

int main()
{
o.f();
}

Another way would be to use the Singleton pattern (do a google on that).

If you explain to us your design, we may be able to help you more.
Jonathan
}
Jul 22 '05 #2
Hi Jonathan,

Jonathan Mcdougall wrote:
If you explain to us your design, we may be able to help you more.


Maybe my design is a little weird. After application startup i want to get
the User ID and the access rights of this user. It has to be accessible to
every class because every class has to check if the user has all the rights
to use it's functions or otherwise deny the access.

Thanks

Sven
Jul 22 '05 #3
"Gunnar Beushausen" <al**************@t-online.de> a écrit dans le message
de news: cq*************@news.t-online.com...
Hi Jonathan,

Jonathan Mcdougall wrote:
If you explain to us your design, we may be able to help you more.


Maybe my design is a little weird. After application startup i want to get
the User ID and the access rights of this user. It has to be accessible to
every class because every class has to check if the user has all the
rights
to use it's functions or otherwise deny the access.


Either use free functions (such as bool check_right(Operation&)) to avoid
fiddling with global data from everywhere, or use a highly protected global
variable UserData (with no modifiers or "setters") which you can use from
your other classes. Restricting the framework from calling sensitive
functions (such as by disabling some widgets at load time) would be better
than checking the rights in each of them, but it may be impossible in your
design.
Jonathan
Jul 22 '05 #4
Gunnar Beushausen wrote:
I need a class to store the users data (ID, name etc.) cat user.h #ifndef GUARD_USER_H
#define GUARD_USER_H 1

class User {
private:
// representation
const char* ID;
const char* Name;
public:
// functions
const char* id(void) const { return ID; }
const char* name(void) const { return Name; }
// constructors
User(const char* i, const char* n):
ID(i), Name(n) { }
};

extern const User gunnar; // global constant
// accessible everywhere user.h is included

#endif//GUARD_USER_H
cat main.h #include 'user.h'

// define global constant
const User gunnar("gunnar", "Gunnar Beushausen");

int main(int argc, char* argv[]) {
// call other routines here.
return 0;
}
that is accessible from anywhere.

At application startup the class gets filled with its data about the user.
But how can I access this data from all other classes?

Normally to get access, I would have to say something like UserData *UD = new UserData;
Maybe in Java but *not* in C++.
But this way a new [objects] gets instantieted.
Objects are instances of classes.
What can I do to access the data from anywhere?


Global variables are a very bad idea.
If you use global variables, Bjarne will track you down
and break your fingers so that you can't code C++ anymore.
Global constants are just fine.
Jul 22 '05 #5
It seems you'd like to try a class with the static attributes, which is
accessible without instantiation.

"Gunnar Beushausen" <al**************@t-online.de> ????
news:cq*************@news.t-online.com...
Hi!

I need a class to store the users data (ID, name etc.) that is accessible
from anywhere.

At application startup the class gets filled with its data about the user.
But how can i access this data from all other classes?

Normally to get access, i would have to say something like UserData *UD =
new UserData; But this way a new class gets instantieted. What can i do to
access the data from anywhere?

Thanks in advance

Sven

Jul 22 '05 #6
ox
> After application startup i want to get the User ID
and the access rights of this user. It has to be
accessible to every class because every class has
to check if the user has all the rights to use
it's functions or otherwise deny the access.


I'd probably use something like the following:

enum privilege_t {
priv_read,
priv_write,
// ...
};

class auth {
public:

static bool authenticate(void);
// gets user ID; returns `true' iff successful

static void authorise(privilege_t priv);
// throws an exception if not authorised

private:

// ...
};

// Now inherit all classes that need authorisation from auth:

class foo : public auth {
public:
void do_something(void) {
authorise(priv_read);
// ... do the reading
}
};

class bar : public auth { /* ... */ };

Jul 22 '05 #7

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

Similar topics

1
by: Jean-Francois Brault | last post by:
I wrote a crappy class for radian angle management. The class consists of an array of radian values. I put all these things in a class in which all methods are static, so I can access it anywhere...
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
2
by: Johnny Meredith | last post by:
Warning: Tenderfoot I'm playing around with ASP.Net 2.0 b2. The web app I've created has a default, login, recovery, etc. pages in the root and two folders accessible to authenticated users...
3
by: Nick Valeontis | last post by:
Hi to all! I am writing an implentation of the a-star algorithm in c#. My message is going to be a little bit long, but this is in order to be as specific as possible. My question has nothing to...
1
by: djss900 | last post by:
I've done some searching but haven't had much luck figuring this one out - hopefully someone here has some insight as to what I might be doing wrong. Assuming we have a regular C# driven asp.net...
3
by: James Booker | last post by:
Hi all - this should be a very easy question to you guys. Say I have two classes, class1 and class2. In my application, class2 can't operate without a class1, as class1 performs communication...
1
by: Bryan Parkoff | last post by:
An object can be defined using a class. The class contains variables and functions. It has a pointer to bind variables and functions. If I want to create more than one object. The class can...
8
by: shapper | last post by:
Hello, I am working with VS 2008 and created a Web Application Project. I added a class but whatever I do the class is not visible to my aspx pages or anywhere else. I then changed the...
3
by: laikon | last post by:
this question is about how to define friend functions for a class template. the following is an example. template <typename T> class Array { private: T* parr; int sz;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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...

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.