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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about scope and variable access 1

Status
Not open for further replies.

Valius

Programmer
Oct 20, 2000
174
US
I've got this pointer that I have defined and initialized in a function (OnInitDialog()) that connects to our phone server. I want to access this pointer again when they click on the disconnect button so I can disconnect it. I could do this through global variables...that's what my previous version did...but what I'm wanting to do is use "references" throughout the whole program to access this pointer. How could I access this variable that is local to the OnInitDialog() function in my disconnect function? I understand the concepts, just not quite how to do it. Could I use extern or something like that? I'm horrible at research and can't seem to find anything that I need eventhough I KNOW that there is stuff out there that explains this. Thanks for any info and your help!

Niky Williams
NTS Marketing
 
Niky,

> How could I access this variable that is local to the OnInitDialog() function
> in my disconnect function?

You can't. 'Scope' also reflects the 'lifetime' of the variable. All function scope variables die at the end of the function.

What it sounds like you want is a 'Singleton'. Singleton is a 'pattern'. They come in real handy for situations like the one you seem to be in. Any person seriously interested in C++ needs to read at least these two books:

More Effective C++ - Scott Meyers
Design Patterns - Gamma, Helm, Johnson, Vlissides

So the Singleton pattern is about having a single instance of a class that is available to any module in the code base by including the header file that defines the object. (This of course means you need to be concerned about re-entrance in a multi-threaded application).

This is how you could do one for you project:

class PhoneSrv{

protected:
PhoneSrv(){}
~PhoneSrv(){}
// the one and ONLY instance of PhoneSrv object
static PhoneSrv* _instance;

void makeCall(const char* number){ /* do something */ }

// allocates our single instance
static PhoneSrv& instance(){
if ( !_instance)
_instance = new PhoneSrv();

return *_instance;
}

public:

static void call( const char* number){ instance().makeCall( number); }

};

// then in the implementation file (.cpp)
#include "PhoneSrv.h"
// declare the static variable
PhoneSrv* PhoneSrv::_instance = NULL;

// then anywhere in your application code
#include "PhoneSrv.h"

PhoneSrv::call("322.5555");

Hope this helps
-pete
 
YES! This is EXACTLY what I was lookin for. I thought about making a class somehow and having variables in there...but I just wasn't sure how to go about it...so what I was working on was just putting some variables in my PhoneDlg.h under public: to access it that way. ACK..got so much to learn! This is just so much different to me than the traditional 16-bit Turbo C++ that I'm so used to. Thank you so much for your help!

Niky Williams
NTS Marketing
 
I've done this, but now I'm getting access violations when I try to access one of the variables in the class....any ideas?


Niky Williams
NTS Marketing
 
Niky,

Access Violations are usually an indication that a 'pointer' is being used before it has been allocated.

In the code I posted the lines that are relevant to that issue are:

// the one and ONLY instance of PhoneSrv object
static PhoneSrv* _instance;

// allocates our single instance
static PhoneSrv& instance(){
if ( !_instance)
_instance = new PhoneSrv();

return *_instance;
}

// declare the static variable
PhoneSrv* PhoneSrv::_instance = NULL;

This last line is extremely important. If the pointer is not initialized to 'NULL' then the if statment will evaluate to false and the object will never be allocated and constructed.

Hope this helps
-pete
 
Hmm...I've done this pretty much as you have said...I've defined some extra variables in the Public: section, included the .h files in my project. The intellisense recognizes it as a class...I've also defined an instance of my class in the function that I want to use it in.

// declare the static variable
PhoneSrv* PhoneSrv::_instance = NULL;

I did this in the implementation file of my "Objects" class, then I defined an instance of this class like this:

Objects *RefObjects;

and tried to set a data member to a value like this:

RefObjects->Version = "test"; (Version is a CString type)

It's when it hits this line that I get my access violation. I'm thinking it has something to do with directly accessing a pointer.

I'm sorry for takin up so much of your time, but you are helping me tremendously. Thanks again!

Niky Williams
NTS Marketing
 
Something I didn't add is when it is giving my that access violation, it stops when it tries to assert that
m_pchData != NULL
 
> Objects *RefObjects;

You can not use a pointer until you have constructed it, i.e.:

RefObjects = new RefObjects(...);

-pete
 
ok, I see now...I'm starting to understand this thing a bit more now....thanks so much again! Quick question though...the last time I mentioned "global" variables, you almost had a heart attack...why? To me, if you use a single variable throughout your whole program, why not make it a global...to me, this would save on overhead and the confusion that pointers can sometimes cause people (especially beginnig VC++ programmers like me :) ) I'm just curious why you shun globals? Thanks again for your time!

Niky Williams

NTS Marketing
 
Niky,

> I'm just curious why you shun globals?

Well, in the old days (hehehe).

But seriously, code maintenance is the primary problem. Large bodies of code with large numbers of global variables tends to be 'error prone' at best.

Years ago I frequently had to work with large scale C programs that used an abundance of global variables. These globals typically provided access to 'ALL' of the data within the application through a cacophony of nested structures, pointers, strings and such. The flow of code is extremely difficult to follow in comparison to object oriented code. You end up with lines of code like this:

strcpy( g_appdata.usepref->psinfo->ppty[ nItem]->fname, sinFName);
strcpy( g_appdata.usepref->psinfo->ppty[ nItem]->lname, sinLName);
strcpy( g_appdata.usepref->psinfo->ppty[ nItem]->email, sinEMail);

Now can you imagine what the code looks like that handles the allocation of all those pointers! Yuck and YUCK!

So to make a long story short, I really REALLY hate globals. And since OOP provides elegant solutions (like singltons) I just prefer not to even think about them.

However, in a small application, there is not any real harm in having a few global variables. Of course in my experience small applications tend to grow into large and then very large applications. If that happens you might curse the day you ever decided to use globals.

"But, that's just my opinion... I could be wrong".
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top