"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
}