What are the the relative merits of putting objects needed globally for the
duration of program execution in these three places?
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...
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.
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...
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.
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
"flect" <fl...@aol.com> wrote in message
news:ulAhkPIy...@TK2MSFTNGP06.phx.gbl...
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.
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
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
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156554024.1...@m73g2000cwd.googlegroups.com...
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
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
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
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.
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...
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
I cant imagine that this is even being discussed.
---
Ajay
Why should there be any globals? Whats wrong in having singletons?
---
Ajay
That's Tom's proposal: CGlobals IS a singleton.
-- David
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
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156873121.3...@m79g2000cwm.googlegroups.com...
Now I am confused. I thought Tom was "put everything in App" guy.
---
Ajay
I am still hoping that this is a dream.
---
Ajay
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156874594.0...@h48g2000cwc.googlegroups.com...
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156874453.5...@m73g2000cwd.googlegroups.com...
AliR.
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156874453.5...@m73g2000cwd.googlegroups.com...
:o)
Tom
"AliR" <Al...@online.nospam> wrote in message
news:44f49115$0$15208$a826...@reader.corenews.com...
"Tom Serface" <tser...@msn.com> wrote in message
news:uNuEz76y...@TK2MSFTNGP06.phx.gbl...
"AliR" <Al...@online.nospam> wrote in message
news:44f4b153$0$15191$a826...@reader.corenews.com...
One symbol.
g_MyVariable;
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
Whats in a name? Just use #define and you can do magic. I guess you
were just missing a pragma.
---
Ajay
LOL, how did I know you would work that in somewhere?
-- David
I'd say neither.
---
Ajay
"Tom Serface" <tser...@msn.com> wrote in message
news:e0udtV4y...@TK2MSFTNGP02.phx.gbl...
Tom
"David Ching" <d...@remove-this.dcsoft.com> wrote in message
news:je4Jg.205$VK...@newssvr33.news.prodigy.com...
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156894858.3...@74g2000cwt.googlegroups.com...
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156895246....@p79g2000cwp.googlegroups.com...
Tom
"Alexander Grigoriev" <al...@earthlink.net> wrote in message
news:uN7xzG%23yGH...@TK2MSFTNGP05.phx.gbl...
But thats what you were promoting couple of days ago along with DavidC.
---
Ajay
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
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:1156958183.4...@i42g2000cwa.googlegroups.com...
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.
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
I think its either 1st one (EC++ and not More EC++) or the Andrescu(Red
cover) book). Most likely its Andrescu(?).
---
Ajay
AliR.
"Mark Randall" <mar...@gEEEEEmail.com> wrote in message
news:ueTujrFz...@TK2MSFTNGP05.phx.gbl...
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
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
http://www.regdeveloper.co.uk/2006/08/08/cplusplus_loops/
Other than accessing the variable, whats the difference between a
global and a static class?
---
Ajay
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
Having read EC++, we all know exactly how loops are used. Besides its
one of those questions that appears frequently during interviews.
---
Ajay
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
You can always use a Pragma to do it the way you want it and keep it
efficient.
---
Ajay
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.
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
for (size_t i = 0; i < msg.lines.size(); i++)
cout << "lol" << endl;
--
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
>
I like it like this:
for (size_t i = 0; i < msg.lines.size(); ++i)
cout << "lol" << endl;
> 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 ;)
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
You dont happen to post on Yahoo Finance boards by any chance?
"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.
Nope... you saw it there too?
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.
<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
"Mark Randall" <mar...@gEEEEEmail.com> wrote in message
news:u7ro3qIz...@TK2MSFTNGP06.phx.gbl...
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
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:uu6$NzJzGH...@TK2MSFTNGP06.phx.gbl...
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...
I guess you missed the Global Loop Construct.
C++? C# does not. Ha... C++ is so pre-911, its terrorist friendly. Its just
not secure.
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
"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:tq7cf25t485pbvnh6...@4ax.com...
"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
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:O9wdDlSz...@TK2MSFTNGP02.phx.gbl...
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:Os6THhSz...@TK2MSFTNGP05.phx.gbl...
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:uYlUNiSz...@TK2MSFTNGP04.phx.gbl...
How unpatriotic? You should never question anything which pertains to
security.
Tom
"Ajay Kalra" <ajay...@yahoo.com> wrote in message
news:eCvZzwSz...@TK2MSFTNGP06.phx.gbl...
Absoluteeeely nothing :o)
But I thought people needed some humour time.
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
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
"Mark Randall" <mar...@gEEEEEmail.com> wrote in message
news:%236w7jkT...@TK2MSFTNGP03.phx.gbl...
This threads all over the place, what were we talking about again? :S
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
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
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 [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
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