Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice on Global classes using MFC Doc/View

Status
Not open for further replies.

milner

Programmer
Joined
Apr 15, 1999
Messages
28
Location
CA
I'm making an application with many dialog windows and classes that perform different actions. I want users to log in to the program before they can do anything.

Different users have different permissions for the programs operations. Every time a button is pressed to do something, I need to check if the current user is allowed to do that action.

What I'm wondering is, should I make a Current User class global to my project? If so, how can I do it and still be using good programming practice (I'd rather not have to pass this object to ALL my functions every time they are called).

I also need to do a similar thing with a logging class. Every action has to add an entry to the log.

Any help or advice is appreciated,

- Milner -
 
Milner,

If this is a single user desktop application you can use the Singleton pattern to solve your problem. If it is a server (multi-user) application that won't work.

A Singleton would work something like this:


class UserSession{

static UserSession* _pInstance;

protected:
UserSession(){}
~UserSession(){}
UserSession( const UserSession& rhs){}
const UserSession& operator=( const UserSession& rhs){return *this;}

static UserSession* instance(){

if ( NULL == _pInstance)
_pInstance = new UserSession();

return _pInstance;
}

// actual member variables and functions
CString _name;
long _id;

public:
// public interface
static void setName(LPCTSTR name){ instance()->_name = name; }
static void setID(long id){ instance()->_id = id; }

static LPCTSTR getName(){ return instance()->_name; }
static long getID(){ return instance()->_id; }

};


In a single .CPP file

// Initialize the one and ONLY UserSession pointer
UserSession* UserSession::_pInstance = NULL;


Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top