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

G

Gunnar Beushausen

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
 
J

Jonathan Mcdougall

Gunnar Beushausen said:
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


}
 
G

Gunnar Beushausen

Hi Jonathan,

Jonathan said:
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
 
J

Jonathan Mcdougall

Gunnar Beushausen said:
Hi Jonathan,



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
 
E

E. Robert Tisdale

Gunnar said:
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.
 
A

Atlas

It seems you'd like to try a class with the static attributes, which is
accessible without instantiation.
 
O

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 { /* ... */ };
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top