469,902 Members | 1,976 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

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 2435
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jean-Francois Brault | last post: by
7 posts views Thread by Morgan Cheng | last post: by
2 posts views Thread by Johnny Meredith | last post: by
3 posts views Thread by James Booker | last post: by
1 post views Thread by Bryan Parkoff | last post: by
8 posts views Thread by shapper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.