Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Should Globally Needed Objects be Members in Main Frame, Application, or be Global?

0 views
Skip to first unread message

Artist

unread,
Aug 25, 2006, 4:00:38 PM8/25/06
to
I have been making objects needed by my entire application members either
the Main Frame ( main window derived from CMDIFrameWnd ) or the Application
(derived from CWinApp ). To get to these members I use either GetMainWnd()
or AfxGetApp() to get pointers I can access them with. Now I wonder if there
is any reason not to make these objects global so the use of GetMainWnd() or
AfxGetApp() can be dispensed with.

What are the the relative merits of putting objects needed globally for the
duration of program execution in these three places?


AliR

unread,
Aug 25, 2006, 4:04:33 PM8/25/06
to
I typically create a singleton or static class for global variables.
Static class is my favorite because it requires the least amount of work.

class CGlobals
{
private:
CGlobal();

public:
static long GetValue();
static void SetValue(long Value);
.....
private:
static long m_Value;
.....
};

AliR.

"Artist" <art...@sj.speakeasy.net> wrote in message
news:eZSdnbJoQIlwy3LZ...@speakeasy.net...

flect

unread,
Aug 25, 2006, 4:20:02 PM8/25/06
to
> I typically create a singleton or static class for global variables.
> Static class is my favorite because it requires the least amount of work.
>
> class CGlobals
> {
> private:
> CGlobal();
>
> public:
> static long GetValue();
> static void SetValue(long Value);
> .....
> private:
> static long m_Value;
> .....
> };
>
> AliR.

I do more or less the same thing, but make it a member of CWinApp. That
way, you can cast AfxGetApp to your app and know exactly what was really
intended to be global.

AliR

unread,
Aug 25, 2006, 4:23:32 PM8/25/06
to
Well the point of the CGlobal class was so that you don't have to do any
casting, and it would definatly tell whoever is reading the code that this
class holds global data.

To access any thing in the global class form anywhere would be

CGlobal::AnyStaticPublicMember

AliR.


"flect" <fl...@aol.com> wrote in message
news:ulAhkPIy...@TK2MSFTNGP06.phx.gbl...

Heinz Ozwirk

unread,
Aug 25, 2006, 4:41:03 PM8/25/06
to
"flect" <fl...@aol.com> schrieb im Newsbeitrag
news:ulAhkPIy...@TK2MSFTNGP06.phx.gbl...

There's no need to cast or use AfxGetApp at all. Just declare "theApp" at
the end of the header defining your CWinApp derived class. That global
variable has already been defined, so all you need is an extern declaration.
As long as you only use theApp in the exe itself, it is easier to use and a
little bit faster than AfxGetApp. Only if code in a dll wants to access the
exe's application object, it must use AfxGetApp.

Heinz

flect

unread,
Aug 25, 2006, 4:55:28 PM8/25/06
to
> There's no need to cast or use AfxGetApp at all. Just declare "theApp"
> at the end of the header defining your CWinApp derived class. That
> global variable has already been defined, so all you need is an extern
> declaration. As long as you only use theApp in the exe itself, it is
> easier to use and a little bit faster than AfxGetApp. Only if code in a
> dll wants to access the exe's application object, it must use AfxGetApp.
>
> Heinz

That sounds interesting. Can you give an example?

As to easier, CThisApp* the_app = (CThisApp*)AfxGetApp(), is pretty easy.

Scott McPhillips [MVP]

unread,
Aug 25, 2006, 5:28:22 PM8/25/06
to
flect wrote:
> That sounds interesting. Can you give an example?
>
> As to easier, CThisApp* the_app = (CThisApp*)AfxGetApp(), is pretty easy.

It's not easy, it's ugly clutter. It can be replaced with...

theApp.

after you add this line to the end of app.h:
extern CThisApp theApp;

--
Scott McPhillips [VC++ MVP]

Tom Serface

unread,
Aug 25, 2006, 5:33:14 PM8/25/06
to
I typically put global variables in the CWinApp as well so they are easy to
get to, but I don't use many globals these days.

Tom

"flect" <fl...@aol.com> wrote in message
news:ulAhkPIy...@TK2MSFTNGP06.phx.gbl...

flect

unread,
Aug 25, 2006, 5:39:11 PM8/25/06
to

It is easier and better.

Hopefully, you learn something new every day ;)

Scott McPhillips [MVP]

unread,
Aug 25, 2006, 5:58:09 PM8/25/06
to
flect wrote:
>> It's not easy, it's ugly clutter. It can be replaced with...
>>
>> theApp.
>>
>> after you add this line to the end of app.h:
>> extern CThisApp theApp;
>>
>
> It is easier and better.
>
> Hopefully, you learn something new every day ;)

Because of the "OO politically incorrect" attitude toward globals
nowadays, nobody seems to teach how to implement globals any more :(

With the invention of C++ constructors global objects became it bit more
dangerous because the order of execution of their constructors is
undefined. To avoid that problem it is probably best, if one has global
objects, to put them in the CWinApp. That makes the construction order
predictable and stable.

Ajay Kalra

unread,
Aug 25, 2006, 9:00:24 PM8/25/06
to

I do exactly the way AliR does. I dont mess with CWinApp either unless
it is related to it (HasA relationship). Keep in mind not all modules
will have access to the App object. For a Regular DLL to access the app
object itself is another task. My advice: unless you have to, dont put
it in App.

---
Ajay

Mark Randall

unread,
Aug 27, 2006, 7:25:08 AM8/27/06
to
I hate to be the black sheep here, but I make a significant number of
objects global (granted they are all classes that are all highly
encapsulated).

The app that I am working on now contains at least 20 global objects, mainly
classes to manage complex lists or processes such as resolving sets of DNS
addresses, and I have yet to encounter any problems at all, and these are
not 'simple' classes either.

As of late due to heavy use of namespace I have also been doing the
following:

namespace SomeControlProcedure
{
class X { ... };
class Y { ... };
class Z { ... };
class Control { ... }; // making use of X, Y, Z etc

extern Control g_Control;
}

I find it great for keeping everything self contained, obviously with less
trivial names, but it keeps the 'global' namespace cleaner, its quicker to
follow, and when these object defenitions dont change much you can drop them
into the standard afx without worrying about name conflicts.

In situations where I do not use this system, I get around the constructor
problem by dumping every variables actual defenition in a single CPP so I
know the order that they will be constructed?

What is the real problems with self-managing global objects? Surely its
better than having to drag a variable out of another variable thats still a
global anyway, or using static members that leave you utterly stuck if you
do ever need to use another instance of a class etc.

</rant> :)

--
- Mark Randall
http://www.temporal-solutions.co.uk
http://www.awportals.com

"Artist" <art...@sj.speakeasy.net> wrote in message
news:eZSdnbJoQIlwy3LZ...@speakeasy.net...

Tom Serface

unread,
Aug 28, 2006, 12:28:01 PM8/28/06
to
I guess we all have "our ways" :o) I use the method Scott suggested (an
extern to the app call theApp) all the time and it's worked find for many
applications. I tend to try to make objects stand alone whenever possible
so I mostly only use this mechanism where I would normally use AfxGetApp()
and I just find it convenient to have one place to put objects I want to
access from anywhere. I certainly wouldn't try to fish data from the
application from a DLL, but I wouldn't call AfxGetApp() from a DLL either.
If you do that, why have the DLL in the first place.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156554024.1...@m73g2000cwd.googlegroups.com...

Ajay Kalra

unread,
Aug 28, 2006, 2:17:38 PM8/28/06
to

Tom Serface wrote:
> I guess we all have "our ways" :o) I use the method Scott suggested (an
> extern to the app call theApp) all the time and it's worked find for many
> applications. I tend to try to make objects stand alone whenever possible
> so I mostly only use this mechanism where I would normally use AfxGetApp()
> and I just find it convenient to have one place to put objects I want to
> access from anywhere. I certainly wouldn't try to fish data from the
> application from a DLL, but I wouldn't call AfxGetApp() from a DLL either.
> If you do that, why have the DLL in the first place.

If we followed this(touching an object which is included in App), it
will take over a day to compile our project. Also, most likely person
who did this would be severely reprimanded. This really has nothing to
do with peculiar ways. Its straight OO programming. Its just simply
more pronounced in a large dev environment.

---
Ajay

Joseph M. Newcomer

unread,
Aug 29, 2006, 11:19:03 AM8/29/06
to
They should definitely NOT be members of the CWinApp class. This requires that the
CWinApp-derived header file be present in all modules that use them. Microsoft loves to
dump this file into every file it generates, and the first thing you should do is delete
it, and replace it by either nothing or by #include "resource.h". As soon as you write
((CMyApp *)AfxGetApp())->
you have made a fundamental error in programming, as far as I'm concerned. The members of
the CWinApp class do not apply.

The same is true for the mainframe class. It has no purpose in holding "global"
variables. It should hold variables that apply to the main frame, and only the main
frame.

If it is truly global, make it global. I have often created a .cpp file that declares
exactly one variable and a .h file that declares it as extern. That's it. The
"globals.h" file is almost always a bad approach to the problem. THe only global
variables that should share a file are global variables that have a logical relationship
with each other. The good news is that in a well-written program, you have very few
global variables anyway, so even with one variable for file, you only have a small number
of files to represent them.

Summary: global variables are global variables, they are not members of some
randomly-selected class such as the CWinApp, CMainFrame, or any other such class. All you
have then is a piece of syntax that that makes your program structure convoluted without
actually adding anything to the solution of the problem.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

David Ching

unread,
Aug 29, 2006, 11:44:41 AM8/29/06
to
"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:v7m8f2h3hs33853t7...@4ax.com...

> The
> "globals.h" file is almost always a bad approach to the problem. THe only
> global
> variables that should share a file are global variables that have a
> logical relationship
> with each other. The good news is that in a well-written program, you
> have very few
> global variables anyway, so even with one variable for file, you only have
> a small number
> of files to represent them.
>

In small programs, it works to have a CGlobals class that contains all user
preferences and also takes care to load, save and show the propertysheet for
setting these preferences. It's really more of a CUserOptions class, but it
is global in the sense that the options need to be referenced by all parts
of the program.

And since this is the case, what would it hurt to have CGlobals be a member
of CWinApp? I don't see anything fundamentally wrong with that.

-- David


AliR

unread,
Aug 29, 2006, 11:53:15 AM8/29/06
to
>
> Summary: global variables are global variables, they are not members of
some
> randomly-selected class such as the CWinApp, CMainFrame, or any other such
class. All you
> have then is a piece of syntax that that makes your program structure
convoluted without
> actually adding anything to the solution of the problem.

I agree with the fact that global variables don't belong in CWinApp or
CMainFrame.
But I disagree just a little. If you decide to have globals they should be
placed in a globals class just make it easier for reading purposes, how
often have you opened up someone else's code and find yourself looking for
the declaration of a variable that is being used in that class?
Which one is better MyVariable or CGlobals::m_MyVariable?

AliR.


Tom Serface

unread,
Aug 29, 2006, 12:08:50 PM8/29/06
to
Hi David,

I think a lot of this is programmer preference. For example, I find it
handy that VS puts the include for the app code in almost every file it
creates. That makes the globals more accessible. I understand Joe's points
too, but this works for me.

Tom

"David Ching" <d...@remove-this.dcsoft.com> wrote in message
news:JhZIg.4258$yO7....@newssvr14.news.prodigy.com...

David Ching

unread,
Aug 29, 2006, 12:21:40 PM8/29/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:e0udtV4y...@TK2MSFTNGP02.phx.gbl...

>
> I think a lot of this is programmer preference. For example, I find it
> handy that VS puts the include for the app code in almost every file it
> creates. That makes the globals more accessible. I understand Joe's
> points too, but this works for me.
>

Yeah, it could go either way for me. Personally, I create a singleton
CGlobals class and use it from anywhere I need to access a global variable.
It's more direct to say

CGlobals::strUserName;

instead of

CMyWinApp::CGlobals::strUserName


I just don't see why Joe says CWinApp isn't the place to put global
variables that are obviously application related. I mean, things like the
command-line is available from CWinApp, why not application global
variables?


-- David


Ajay Kalra

unread,
Aug 29, 2006, 1:38:41 PM8/29/06
to
> They should definitely NOT be members of the CWinApp class.

I cant imagine that this is even being discussed.

---
Ajay

Ajay Kalra

unread,
Aug 29, 2006, 1:40:47 PM8/29/06
to
> But I disagree just a little. If you decide to have globals they should be
> placed in a globals class just make it easier for reading purposes, how
> often have you opened up someone else's code and find yourself looking for
> the declaration of a variable that is being used in that class?
> Which one is better MyVariable or CGlobals::m_MyVariable?
>

Why should there be any globals? Whats wrong in having singletons?


---
Ajay

David Ching

unread,
Aug 29, 2006, 1:44:14 PM8/29/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156873247....@i3g2000cwc.googlegroups.com...

>> Which one is better MyVariable or CGlobals::m_MyVariable?
>>
>
> Why should there be any globals? Whats wrong in having singletons?
>

That's Tom's proposal: CGlobals IS a singleton.

-- David


Ajay Kalra

unread,
Aug 29, 2006, 1:51:38 PM8/29/06
to

David Ching wrote:
> "Tom Serface" <tser...@msn.com> wrote in message
> news:e0udtV4y...@TK2MSFTNGP02.phx.gbl...
> >
> > I think a lot of this is programmer preference. For example, I find it
> > handy that VS puts the include for the app code in almost every file it
> > creates. That makes the globals more accessible. I understand Joe's
> > points too, but this works for me.
> >
>
> Yeah, it could go either way for me. Personally, I create a singleton
> CGlobals class and use it from anywhere I need to access a global variable.
> It's more direct to say
>
> CGlobals::strUserName;

Thats the way to do it. I however never expose the variable directly; I
provide an accessor method.

>
> I just don't see why Joe says CWinApp isn't the place to put global
> variables that are obviously application related. I mean, things like the
> command-line is available from CWinApp, why not application global
> variables?

There is nothing wrong in this. If variables have a HasA relationship
with App, its should be in there.

Point of contention is to put any global variable in CWinApp because of
it works or its easy. Singletons take care of this in a very OOP way
and should be used.

---
Ajay

Tom Serface

unread,
Aug 29, 2006, 1:55:09 PM8/29/06
to
It's easy to imagine since it is being discussed :o)

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156873121.3...@m79g2000cwm.googlegroups.com...

Ajay Kalra

unread,
Aug 29, 2006, 2:00:53 PM8/29/06
to
> That's Tom's proposal: CGlobals IS a singleton.

Now I am confused. I thought Tom was "put everything in App" guy.

---
Ajay

Ajay Kalra

unread,
Aug 29, 2006, 2:03:14 PM8/29/06
to
> It's easy to imagine since it is being discussed :o)
>

I am still hoping that this is a dream.

---
Ajay

Tom Serface

unread,
Aug 29, 2006, 2:06:51 PM8/29/06
to
And then "poof" you woke up.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156874594.0...@h48g2000cwc.googlegroups.com...

Tom Serface

unread,
Aug 29, 2006, 2:06:23 PM8/29/06
to
I've actually done it both ways, but I prefer putting them in the
application. I am not a fan of singletons as I find them difficult to
debug. But, to be fair, I am more concerned with writing code that works
and getting it done quickly than I am the latest in programming styles.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156874453.5...@m73g2000cwd.googlegroups.com...

AliR

unread,
Aug 29, 2006, 3:08:36 PM8/29/06
to
I think he ment AliR. CGlobal is either a singleton or a static class.
What I was disagreeing with was that Joe was saying to just make the globals
global.

AliR.

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156874453.5...@m73g2000cwd.googlegroups.com...

Tom Serface

unread,
Aug 29, 2006, 5:06:07 PM8/29/06
to
You know, AliR ... Tom ... it's easy to get them mixed up since the names
are so similar.

:o)

Tom

"AliR" <Al...@online.nospam> wrote in message
news:44f49115$0$15208$a826...@reader.corenews.com...

AliR

unread,
Aug 29, 2006, 5:26:10 PM8/29/06
to
We are practically brothers. ;)

"Tom Serface" <tser...@msn.com> wrote in message

news:uNuEz76y...@TK2MSFTNGP06.phx.gbl...

Tom Serface

unread,
Aug 29, 2006, 6:11:07 PM8/29/06
to
Indeed!

"AliR" <Al...@online.nospam> wrote in message

news:44f4b153$0$15191$a826...@reader.corenews.com...

Mark Randall

unread,
Aug 29, 2006, 6:50:51 PM8/29/06
to
"AliR" <Al...@online.nospam> wrote in message news:44f4634d$0$15209

> Which one is better MyVariable or CGlobals::m_MyVariable?

One symbol.

g_MyVariable;

David Ching

unread,
Aug 29, 2006, 7:38:55 PM8/29/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:uNuEz76y...@TK2MSFTNGP06.phx.gbl...
> You know, AliR ... Tom ... it's easy to get them mixed up since the names
> are so similar.
>
> :o)
>

Sorry, I knew it was one of the more prolific posters here, I guess I got
mixed up and was too lazy to make certain before naming names. My bad.

-- David


Ajay Kalra

unread,
Aug 29, 2006, 7:40:58 PM8/29/06
to

Whats in a name? Just use #define and you can do magic. I guess you
were just missing a pragma.

---
Ajay

David Ching

unread,
Aug 29, 2006, 7:42:13 PM8/29/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156894858.3...@74g2000cwt.googlegroups.com...

>
> Whats in a name? Just use #define and you can do magic. I guess you
> were just missing a pragma.
>

LOL, how did I know you would work that in somewhere?

-- David


Ajay Kalra

unread,
Aug 29, 2006, 7:47:26 PM8/29/06
to
> "AliR" <Al...@online.nospam> wrote in message news:44f4634d$0$15209
> > Which one is better MyVariable or CGlobals::m_MyVariable?
>
> One symbol.
>
> g_MyVariable;

I'd say neither.

---
Ajay

Alexander Grigoriev

unread,
Aug 29, 2006, 11:09:25 PM8/29/06
to
Such #include of the app.h file makes code reuse quite difficult.

"Tom Serface" <tser...@msn.com> wrote in message

news:e0udtV4y...@TK2MSFTNGP02.phx.gbl...

Tom Serface

unread,
Aug 30, 2006, 12:14:42 PM8/30/06
to
No problem. Like Ali said, we're like brothers. :o)

Tom

"David Ching" <d...@remove-this.dcsoft.com> wrote in message

news:je4Jg.205$VK...@newssvr33.news.prodigy.com...

Tom Serface

unread,
Aug 30, 2006, 12:15:18 PM8/30/06
to
I don't want my lib linked in by a pragma thank you very much.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156894858.3...@74g2000cwt.googlegroups.com...

Tom Serface

unread,
Aug 30, 2006, 12:16:36 PM8/30/06
to
I'd say what ever works for you and gives you stable code and easy
maintenance in your work environment. I do like the g_ syntax for globals,
but I'd assume those would be statically global, not members of a class. I
will go on the record as not liking to use those unless there is no other
way around it.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156895246....@p79g2000cwp.googlegroups.com...

Tom Serface

unread,
Aug 30, 2006, 12:18:18 PM8/30/06
to
I'm not sure I understand your point. I guess if you mean it's more
difficult to copy from project to project you may be correct in that,
however, I've never found it that difficult to replace the headers for files
I want to copy. I guess if you wanted to share code you could have another
header (something like project.h) that you include that would include the
correct project. I think that would make it even more confusing though.

Tom

"Alexander Grigoriev" <al...@earthlink.net> wrote in message
news:uN7xzG%23yGH...@TK2MSFTNGP05.phx.gbl...

Ajay Kalra

unread,
Aug 30, 2006, 1:08:11 PM8/30/06
to
> I don't want my lib linked in by a pragma thank you very much.
>

But thats what you were promoting couple of days ago along with DavidC.

---
Ajay

Ajay Kalra

unread,
Aug 30, 2006, 1:16:23 PM8/30/06
to

Tom Serface wrote:
> I'd say what ever works for you and gives you stable code and easy
> maintenance in your work environment. I do like the g_ syntax for globals,
> but I'd assume those would be statically global, not members of a class. I
> will go on the record as not liking to use those unless there is no other
> way around it.

I exclusively use Singletons and advocate strong use of it, especially
in large projects. More importantly, I create the object (what you call
global object) only when called on the method to access it. This is a
key difference: all other options will create the object when app comes
live. The method I use does not do that. This was discussed in one of
C++ books at length (either SMeyers or Anderescu(?)).

---
Ajay

Tom Serface

unread,
Aug 30, 2006, 1:33:56 PM8/30/06
to
Do you mean the Effective C++ books by Scott Meyers?

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156958183.4...@i42g2000cwa.googlegroups.com...

Mark Randall

unread,
Aug 30, 2006, 1:36:55 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote:
> I exclusively use Singletons and advocate strong use of it, especially
> in large projects. More importantly, I create the object (what you call
> global object) only when called on the method to access it.

Why would that have any benefit? With a system where you have different
logic flow it could mean that your constructor is called at god knows what
part of the program execution.

Personally, and this is just me, I favour explicitly calling an Init method
on my global classes on program start up, hence having full control over
over order in an easy way.

Ajay Kalra

unread,
Aug 30, 2006, 1:40:16 PM8/30/06
to
> > I exclusively use Singletons and advocate strong use of it, especially
> > in large projects. More importantly, I create the object (what you call
> > global object) only when called on the method to access it.
>
> Why would that have any benefit?

An object does not created if its not needed.

> With a system where you have different
> logic flow it could mean that your constructor is called at god knows what
> part of the program execution.

Why? It will be called exactly when you want it to be called.


> Personally, and this is just me, I favour explicitly calling an Init method
> on my global classes on program start up, hence having full control over
> over order in an easy way.

You dont give this up at all. Same thing happens in the way I described
it. Construction is deterministic except that its not done when global
objects are created but done when these objects are needed.

---
Ajay

Ajay Kalra

unread,
Aug 30, 2006, 1:41:34 PM8/30/06
to
> Do you mean the Effective C++ books by Scott Meyers?
>

I think its either 1st one (EC++ and not More EC++) or the Andrescu(Red
cover) book). Most likely its Andrescu(?).

---
Ajay

AliR

unread,
Aug 30, 2006, 2:24:05 PM8/30/06
to
That's why I prefer a static class over a singleton for globals.

AliR.

"Mark Randall" <mar...@gEEEEEmail.com> wrote in message
news:ueTujrFz...@TK2MSFTNGP05.phx.gbl...

David Ching

unread,
Aug 30, 2006, 3:47:40 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156958183.4...@i42g2000cwa.googlegroups.com...

>
> More importantly, I create the object (what you call
> global object) only when called on the method to access it. This is a
> key difference: all other options will create the object when app comes
> live. The method I use does not do that. This was discussed in one of
> C++ books at length (either SMeyers or Anderescu(?)).
>

This is more efficient but you can't use CGlobals::m_strVariable syntax; you
need something like CGlobals::Instance()->m_strVariable which is kind of
lame. Since globals are generally in scope for the life of the app, I go
with the first one.

-- David

Joseph M. Newcomer

unread,
Aug 30, 2006, 6:21:48 PM8/30/06
to
Like you, when I have a set of related options I'll do a class that holds them, and
understands how to read and write them.

Note that the command line is only used in the CWinApp class, so its presence doesn't
matter to the rest of the application.

Remember, I *literally* come from the school of "global variables considered harmful"
(that paper was co-authored by two members of my PhD committee, Bill Wulf and Mary Shaw),
so I limit the number of global variables to those which really represent program state.
Then I include the header files that define those variables only in the modules that use
them.

Also, I was on the thesis committee of a student who proved that doing things like
allowing the app header file to be included everywhere is a Really Bad Idea (Ellen
Borison, "Program changes and the cost of selective recompilation". Ph.D. thesis, Dept. of
Computer. Science, Carnegie-Mellon. University, Pittsburgh, PA, 1989). Unlike my opinion
that it is a bad idea, she proved quantitatively that it is a bad idea.

It is primarily an issue of modularity. If module A communicates with module B, then
there should be a clean communication path between them; they should not, willy-nilly,
share some variable in the CWinApp class. It just doesn't make sense from a modularity
viewpoint.

joe


On Tue, 29 Aug 2006 16:21:40 GMT, "David Ching" <d...@remove-this.dcsoft.com> wrote:

>"Tom Serface" <tser...@msn.com> wrote in message
>news:e0udtV4y...@TK2MSFTNGP02.phx.gbl...
>>

>> I think a lot of this is programmer preference. For example, I find it
>> handy that VS puts the include for the app code in almost every file it
>> creates. That makes the globals more accessible. I understand Joe's
>> points too, but this works for me.
>>
>

>Yeah, it could go either way for me. Personally, I create a singleton
>CGlobals class and use it from anywhere I need to access a global variable.
>It's more direct to say
>
> CGlobals::strUserName;
>
>instead of
>
> CMyWinApp::CGlobals::strUserName
>
>
>I just don't see why Joe says CWinApp isn't the place to put global
>variables that are obviously application related. I mean, things like the
>command-line is available from CWinApp, why not application global
>variables?
>
>
>-- David
>
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Mark Randall

unread,
Aug 30, 2006, 7:19:12 PM8/30/06
to
We should be arguing about for loops instead.

http://www.regdeveloper.co.uk/2006/08/08/cplusplus_loops/

Ajay Kalra

unread,
Aug 30, 2006, 7:23:28 PM8/30/06
to
> That's why I prefer a static class over a singleton for globals.
>

Other than accessing the variable, whats the difference between a
global and a static class?

---
Ajay

Ajay Kalra

unread,
Aug 30, 2006, 7:25:11 PM8/30/06
to
> This is more efficient but you can't use CGlobals::m_strVariable syntax;

The idea is to avoid it.

> you need something like CGlobals::Instance()->m_strVariable

How about CGlobars::Instance().GetStrVariable() and create that
variable when method is first called. Each subsequent time, simply
return already cretaed variable.

---
Ajay

Ajay Kalra

unread,
Aug 30, 2006, 7:26:53 PM8/30/06
to
> We should be arguing about for loops instead.
>
> http://www.regdeveloper.co.uk/2006/08/08/cplusplus_loops/
>

Having read EC++, we all know exactly how loops are used. Besides its
one of those questions that appears frequently during interviews.

---
Ajay

David Ching

unread,
Aug 30, 2006, 7:36:51 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156980311.3...@p79g2000cwp.googlegroups.com...

I like access to my "global" variables and I want them with no fuss.
CGlobals::m_strVariable fits the bill. The other approaches involve more
typing and are less readable, for benefit that I would not use.

-- David


Ajay Kalra

unread,
Aug 30, 2006, 7:40:49 PM8/30/06
to
>
> I like access to my "global" variables and I want them with no fuss.
> CGlobals::m_strVariable fits the bill. The other approaches involve more
> typing and are less readable, for benefit that I would not use.
>

You can always use a Pragma to do it the way you want it and keep it
efficient.

---
Ajay

Joseph M. Newcomer

unread,
Aug 30, 2006, 7:29:57 PM8/30/06
to
But there should be no reason to include the project file!
joe

Ed Weir (ComCast)

unread,
Aug 30, 2006, 8:39:59 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156980413.9...@m79g2000cwm.googlegroups.com...

No, I got it - check this out:

After getting all of Pope Benedict's luggage loaded into the limo, (and he
doesn't travel light), the driver notices that the Pope is still standing on
the curb. "Excuse me, Your Holiness," says the driver," Would you please
take your seat so we can leave?"

"Well, to tell you the truth," says the Pope, "they never let me drive at
the Vatican when I was a cardinal, and I'd really like to drive today. "I'm
sorry, Your Holiness, but I cannot let you do that. I'd lose my job! And
what if something should happen?" protests the driver, wishing he'd never
gone to work that morning. "Who's going to tell? Besides, there might be
something extra in it for you," says the Pope with a smile. Reluctantly, the
driver gets in the back as the Pope climbs in behind the wheel. The driver
quickly regrets his decision when, after exiting the airport, the Pontiff
floors it, accelerating the limo to 105 mph. (Remember, he's a German Pope.)
"Please slow down, Your Holiness!!!" pleads the worried driver, but the Pope
keeps the pedal to the metal until they hear sirens. "Oh, dear God, I'm
gonna lose my license -- and my job!" moans the driver. The Pope pulls over
and rolls down the window as the cop approaches, but the cop takes one look
at him, goes back to his motorcycle, and gets on the radio. "I need to talk
to the Chief," he says to the dispatcher. The Chief gets on the radio and
the cop tells him that he's stopped a limo going a hundred and five. "So
bust him," says the Chief. "I don't think we want to do that, he's really
important," said the cop. The Chief exclaimed," All the more reason!" "No, I
mean really important," said the cop with a bit of persistence. The Chief
then asked, "Who ya got there, the Mayor?" Cop: "Bigger." Chief:" Governor?"
Cop: "Bigger." "Well," said the Chief, "Who is it?" Cop: "I think it's God!"
The Chief is even more puzzled and curious: "What makes you think it's God?"
Cop: "He's got the Pope as a chauffeur.

Ajay Kalra

unread,
Aug 30, 2006, 8:47:37 PM8/30/06
to


Ok. That was good. But I still cant make the Loop connection.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


Ed Weir (ComCast)

unread,
Aug 30, 2006, 9:02:44 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:upQPOcJz...@TK2MSFTNGP04.phx.gbl...
[first attempt snipped]

| Ok. That was good. But I still cant make the Loop connection.
|
| --
| Ajay Kalra [MVP - VC++]
| ajay...@yahoo.com

Ohhhhh Kaaaayyyy....

How about:

A priest, a Pentecostal preacher and a Rabbi all served as chaplains to the
students of Northern Michigan University in Marquette. They would get
together two or three times a week for coffee and to talk shop.

One day, someone made the comment that preaching to people isn't really all
that hard. A real challenge would be to preach to a bear.

One thing led to another and they decided to do an experiment they would all
go out into the woods, find a bear, preach to it, and attempt to convert it.

Seven days later, they're all together to discuss the experience.

Father Flannery, who has his arm in a sling, is on crutches, and has various
bandages, goes first. "Well," he says, "I went into the woods to find me a
bear. And when I found him I began to read to him from the Catechism. Well,
that bear wanted nothing to do with me and began to slap me around. So I
quickly grabbed my ! holy wa ter, sprinkled him and, Holy Mary Mother of
God, he became s gentle a lamb. The bishop is coming out next week to give
him first communion and confirmation."

Reverend Billy Bob spoke next. He was in a wheelchair, with an arm and both
legs in casts, and an IV drip. In his best fire and brimstone oratory he
claimed, "WELL brothers, you KNOW that we don't sprinkle! I went OUT and I
FOUND me a bear. And then I began to READ to my bear from God's HOLY WORD!
But that bear wanted NOTHING to do with me. So I took HOLD of him and we
began to wrestle. We wrestled DOWN one hill, UP another and DOWN another
until we came to a creek. So I quick DUNKED him and BAPTIZED his hairy soul.
And just like you said, he BECAME as gentle as a LAMB. We spent the rest of
the day PRAISING JESUS."

They both looked down at the rabbi, who was lying in a hospital bed. He was
in a body cast and traction with IV's and monitors running in and out of
him. He was in bad shape.

The rabbi looks up and says, "Looking back on it, circumcision may not have
been the best way to start."

See the common thread yet...?
;^)

-- Ed.

-----------------------------------------------------
In dictatorships, you need courage to fight evil; in the free world, you
need courage to see the evil."
—Natan Sharansky

F9E7707A2AF502D0A899C6ACB43A2D35EB7E->bin->b64

Mark Randall

unread,
Aug 30, 2006, 9:05:26 PM8/30/06
to
Ajay Kalra" <ajay...@yahoo.com> wrote:
> Ok. That was good. But I still cant make the Loop connection.

for (size_t i = 0; i < msg.lines.size(); i++)
cout << "lol" << endl;

Ajay Kalra

unread,
Aug 30, 2006, 9:07:58 PM8/30/06
to
I recall reading this earlier here(or somewhere else not long ago). I
believe you must have posted it already.

--
Ajay Kalra [MVP - VC++]
ajay...@yahoo.com


"Ed Weir (ComCast)" <An...@Maus.duh> wrote in message
news:5qGdncKQBsmoqGvZ...@comcast.com...

> 湧atan Sharansky
>
> F9E7707A2AF502D0A899C6ACB43A2D35EB7E->bin->b64
>


Ajay Kalra

unread,
Aug 30, 2006, 9:10:44 PM8/30/06
to
"Mark Randall" <mar...@gEEEEEmail.com> wrote in message
news:eEBdMmJz...@TK2MSFTNGP06.phx.gbl...

> Ajay Kalra" <ajay...@yahoo.com> wrote:
> > Ok. That was good. But I still cant make the Loop connection.
>

I like it like this:

for (size_t i = 0; i < msg.lines.size(); ++i)
cout << "lol" << endl;

Mark Randall

unread,
Aug 30, 2006, 9:18:03 PM8/30/06
to
"Ajay Kalra" wrote:

> I like it like this:
> for (size_t i = 0; i < msg.lines.size(); ++i)
> cout << "lol" << endl;

I want to date Keira Knightley, but I just don't see it happening ;)

Ed Weir (ComCast)

unread,
Aug 30, 2006, 9:24:23 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:ukFCmnJz...@TK2MSFTNGP03.phx.gbl...

|I recall reading this earlier here(or somewhere else not long ago). I
| believe you must have posted it already.

Indeed... sorry, but it looks like Mark just posted a far more succinct
answer than I just did. My bad. Basically some people are fundamentally
religious about C++, and it's just plain funny at times. Like God riding in
a limo or a rabbi trying to circumcise a bear.


Oh well never mind

Ajay Kalra

unread,
Aug 30, 2006, 9:27:26 PM8/30/06
to
"Ed Weir (ComCast)" <An...@Maus.duh> wrote in message
news:mr6dnYto-Orap2vZ...@comcast.com...

You dont happen to post on Yahoo Finance boards by any chance?

Ajay Kalra

unread,
Aug 30, 2006, 9:28:46 PM8/30/06
to


"Mark Randall" <mar...@gEEEEEmail.com> wrote in message

news:OJWKPtJz...@TK2MSFTNGP04.phx.gbl...


> "Ajay Kalra" wrote:
>
> > I like it like this:
> > for (size_t i = 0; i < msg.lines.size(); ++i)
> > cout << "lol" << endl;
>
> I want to date Keira Knightley, but I just don't see it happening ;)

If you stop using global variables, I see that indeed may happen, espically
if you use Singletons instead.

Ed Weir (ComCast)

unread,
Aug 30, 2006, 9:35:10 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:e0iPeyJz...@TK2MSFTNGP05.phx.gbl...

Nope... you saw it there too?

Ajay Kalra

unread,
Aug 30, 2006, 9:40:24 PM8/30/06
to
"Ed Weir (ComCast)" <An...@Maus.duh> wrote in message
news:icadnUpQfKZToWvZ...@comcast.com...

Dont know. Theres a guy/gal there on a specific board who keeps posting
jokes like this. I just wanted to make sure if it was you.

Mark Randall

unread,
Aug 30, 2006, 9:43:14 PM8/30/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote:
> If you stop using global variables, I see that indeed may happen,
> espically
> if you use Singletons instead.

<G>

If I was THAT obsessive about the layout of my code there'd be something
single, but it wouldn't be a singleton design pattern ;)

But...

Ladies, I AM single...

... But I am *NOT* obsessive about code layouts...

...and I sure as hell WONT ask you if your breasts were created when you
were first born, or if they were magically created when I first accessed
them! :)

Come get me Kiera!

Tom Serface

unread,
Aug 31, 2006, 2:00:05 PM8/31/06
to
Interesting read, but what does it have to do with global variables?

Tom

"Mark Randall" <mar...@gEEEEEmail.com> wrote in message

news:u7ro3qIz...@TK2MSFTNGP06.phx.gbl...

Tom Serface

unread,
Aug 31, 2006, 2:01:32 PM8/31/06
to
LOL!

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:e0iPeyJz...@TK2MSFTNGP05.phx.gbl...

Tom Serface

unread,
Aug 31, 2006, 2:01:03 PM8/31/06
to
No.

Tom

"Ed Weir (ComCast)" <An...@Maus.duh> wrote in message

news:5qGdncKQBsmoqGvZ...@comcast.com...


>
> See the common thread yet...?
> ;^)
>
> -- Ed.
>
> -----------------------------------------------------
> In dictatorships, you need courage to fight evil; in the free world, you
> need courage to see the evil."

Tom Serface

unread,
Aug 31, 2006, 2:03:01 PM8/31/06
to
You have to wonder. If global variables are such a bad thing, why does C++
allow them? A lot of the stuff in this thread sounds like religion to me.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:uu6$NzJzGH...@TK2MSFTNGP06.phx.gbl...

Tom Serface

unread,
Aug 31, 2006, 2:03:59 PM8/31/06
to
Mark,

You need to set your sites a little higher in both loop design and stalking.

Tom

"Mark Randall" <mar...@gEEEEEmail.com> wrote in message

news:e5cPU7Jz...@TK2MSFTNGP02.phx.gbl...

Ajay Kalra

unread,
Aug 31, 2006, 2:05:05 PM8/31/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:uHldtdS...@TK2MSFTNGP05.phx.gbl...
> No.
>

I guess you missed the Global Loop Construct.

Ajay Kalra

unread,
Aug 31, 2006, 2:07:03 PM8/31/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:Oxh9zeSz...@TK2MSFTNGP05.phx.gbl...

> You have to wonder. If global variables are such a bad thing, why does
C++
> allow them?

C++? C# does not. Ha... C++ is so pre-911, its terrorist friendly. Its just
not secure.

Tom Serface

unread,
Aug 31, 2006, 2:10:52 PM8/31/06
to
I don't see any difference except it's more difficult to access singletons.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:1156980207.9...@i42g2000cwa.googlegroups.com...
>> That's why I prefer a static class over a singleton for globals.
>>
>
> Other than accessing the variable, whats the difference between a
> global and a static class?
>
> ---
> Ajay
>


Tom Serface

unread,
Aug 31, 2006, 2:13:03 PM8/31/06
to
There isn't really. If you want to copy some code from one project to
another it's easy enough to modify the includes. Most of my projects are
pretty stand alone so this works OK for me. If I do have shared code (which
Visual Source Safe is lousy at handling so I try to stay away from doing
this) I just make sure those files don't need any of the project files.

Tom

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:tq7cf25t485pbvnh6...@4ax.com...

Ajay Kalra

unread,
Aug 31, 2006, 2:12:08 PM8/31/06
to


"Tom Serface" <tser...@msn.com> wrote in message

news:#fABNjSz...@TK2MSFTNGP03.phx.gbl...


> it's more difficult to access singletons.

?????

Typing theApp->myStrVar is so much easier than CGlobals::Instance().GetVar()
?

Tom Serface

unread,
Aug 31, 2006, 2:28:12 PM8/31/06
to
Actually that's theApp.myStrVar to you, but yeah, it's easier.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:O9wdDlSz...@TK2MSFTNGP02.phx.gbl...

Tom Serface

unread,
Aug 31, 2006, 2:29:37 PM8/31/06
to
Yeah, I guess I did. I'll try to catch it on the next iteration.

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:Os6THhSz...@TK2MSFTNGP05.phx.gbl...

Tom Serface

unread,
Aug 31, 2006, 2:30:22 PM8/31/06
to
So you think using global variables makes one a terrorist? I'm not sure I
get that reference :o)

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:uYlUNiSz...@TK2MSFTNGP04.phx.gbl...

Ajay Kalra

unread,
Aug 31, 2006, 2:33:10 PM8/31/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:u577FuSz...@TK2MSFTNGP05.phx.gbl...

> So you think using global variables makes one a terrorist? I'm not sure I
> get that reference :o)

How unpatriotic? You should never question anything which pertains to
security.

Tom Serface

unread,
Aug 31, 2006, 2:39:26 PM8/31/06
to
Sorry... I still ascribe to the old adage about "Security is the
responsibility of the individual".

Tom

"Ajay Kalra" <ajay...@yahoo.com> wrote in message

news:eCvZzwSz...@TK2MSFTNGP06.phx.gbl...

Mark Randall

unread,
Aug 31, 2006, 4:06:27 PM8/31/06
to
Tom Serface" wrote:
> Interesting read, but what does it have to do with global variables?

Absoluteeeely nothing :o)

But I thought people needed some humour time.

Mark Randall

unread,
Aug 31, 2006, 4:07:51 PM8/31/06
to
>Tom Serface" wrote:
> You need to set your sites a little higher in both loop design and
> stalking.

Ut oh, some people in here would try to crucify me if I mentioned how I use
Macro's to set up itterator loops :O

Ed Weir (ComCast)

unread,
Aug 31, 2006, 4:16:17 PM8/31/06
to
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:uYlUNiSz...@TK2MSFTNGP04.phx.gbl...

DAMMIT, Ajay - I was sipping coffee when I read that!!

Ajay Kalra

unread,
Aug 31, 2006, 4:20:41 PM8/31/06
to
> DAMMIT, Ajay - I was sipping coffee when I read that!!

Sorry Dude. But if you get some donuts, I can get some sort of Fascism and
blame it on C++.

Tom Serface

unread,
Aug 31, 2006, 4:38:54 PM8/31/06
to
Don't go there.

Tom

"Mark Randall" <mar...@gEEEEEmail.com> wrote in message

news:%236w7jkT...@TK2MSFTNGP03.phx.gbl...

Mark Randall

unread,
Aug 31, 2006, 4:46:56 PM8/31/06
to
"Tom Serface" <tser...@msn.com> wrote in message
news:ubx291Tz...@TK2MSFTNGP02.phx.gbl...
> Don't go there.

This threads all over the place, what were we talking about again? :S

Tom Serface

unread,
Aug 31, 2006, 4:40:01 PM8/31/06
to
That'll learn you :o) Trustworthy computing is worth it.

Tom

"Ed Weir (ComCast)" <An...@Maus.duh> wrote in message

news:cJadnSiElZkO3mrZ...@comcast.com...


> "Ajay Kalra" <ajay...@yahoo.com> wrote in message
> news:uYlUNiSz...@TK2MSFTNGP04.phx.gbl...

> | C++? C# does not. Ha... C++ is so pre-911, its terrorist friendly. Its

David Wilkinson

unread,
Sep 1, 2006, 11:22:56 AM9/1/06
to
Tom Serface wrote:

Tom:

Yes, I have thought of your "project.h" idea, but never got around to
implementing it. Of course "resource.h" is already a generic name, and
is often an adequate substitute for the application header.

But I think the real reason not to include the app header in every
implementation file is that it creates unnecessary compilation
dependency. In a big project, making a change in MyApp.h can kill you.

David Wilkinson

Tom Serface

unread,
Sep 1, 2006, 12:26:54 PM9/1/06
to
Hi David,

You make a good point, but I've done some pretty big projects and never had
a problem with it. Fortunately, the paradigm allows each of us the freedom
to do it our way (sort of like Burger King).

Tom

"David Wilkinson" <no-r...@effisols.com> wrote in message
news:%23VpN7pd...@TK2MSFTNGP03.phx.gbl...

Joseph M. Newcomer

unread,
Sep 1, 2006, 3:25:15 PM9/1/06
to
See Ellen Borison's thesis, which shows *quantitatively* that this is *exactly* what
happens, and in fact has a negative impact on both development time and project
complexity.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Gerry Quinn

unread,
Sep 3, 2006, 11:38:22 AM9/3/06
to
In article <e0udtV4y...@TK2MSFTNGP02.phx.gbl>, tser...@msn.com
says...
> Hi David,
>
> I think a lot of this is programmer preference. For example, I find it
> handy that VS puts the include for the app code in almost every file it
> creates. That makes the globals more accessible. I understand Joe's points
> too, but this works for me.

It's a pain when you re-use your classes in other projects.

Not a very big pain, I will grant, but a pain nonetheless.

- Gerry Quinn

0 new messages