472,356 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 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 2599
"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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.