How to make all-files-global variable?
Hello, I have a project that has a lot of dialogs.
In the constructor of the MainWindow I check a file in order to set the language. If, e.g. the language is Spanish the program turns to Spanish, if it is English it turns into English etc. The thing is that I have to do this in every single constructor of every dialog in order to check the language and do the appropriate changes in every dialog.
Is it possible to use a all-files-global variable ? I don't want to perform the same check several times.
(e.g.
Code:
//this will be at the constructor of the MainWindow (mainwindow.cpp)
if(the.file.says.the.program.to.be.english) english=1 //from here the other constructors of the other dialogs will check if english==1 and if so they'll do the changes
)
Thx in advance
Re: How to make all-files-global variable?
If it is only for translation you should have a look at Qt internationalization functions. Otherwise have a look at [WIKI]Singleton Pattern[/WIKI].
EDIT: Or use global functions/variables... But I am fine with the singleton pattern:D
Re: How to make all-files-global variable?
Ok, I simply want a fully global variale. I don't want to give singleton a chance right now, so how do I set a "fully" global value?
Re: How to make all-files-global variable?
Well there are different approaches one is described here: http://www.learncpp.com/cpp-tutorial...bal-variables/. Or you can use global functions in a separate file with static variables and then do something like
Code:
int languageCode()
{
static int code = -1;
if (-1 == code)
{
// calculate code
}
return code;
}
so the calculation will also only be done once.
Re: How to make all-files-global variable?
There is no concept of "fully global" variable. The variable is either global or not.
http://www.cplusplus.com/doc/tutorial/variables/
Re: How to make all-files-global variable?
Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
Anyway, I'll have a try at the singleton. Thanx both
Re: How to make all-files-global variable?
Quote:
Originally Posted by
hakermania
Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
Hmm?? Did you follow the link I gave you?
Re: How to make all-files-global variable?
Quote:
Originally Posted by
hakermania
Ok, I just thought than in 2010 somebody would have though about variables than can be used everywhere and can be created in a simple way....
Think about the future. Sure, you only want one variable now, but in a week, month or so you may two variables. Do you change your strategy (and the code already written for the first variable) or do something that is scalable? Thats what people are trying to teach you.
If you are not interested in that, fine, wysota gave you a perfectly usable link for creating a simple global variable.
Just note that simple global variables are fine for "bedroom coding", but are typically rejected for "workplace coding" and most multi-author and large-scale open-source projects.
Re: How to make all-files-global variable?
Singletons are vastly overrated and overused; in reality, they are nothing more than global variables in disguise, and they carry all of the shortcomings of globals along with them while hiding behind an OO veneer. And they bring their own problems to the table, as well.
Re: How to make all-files-global variable?
Quote:
Originally Posted by
SixDegrees
Singletons are vastly overrated and overused; in reality, they are nothing more than global variables in disguise, and they carry all of the shortcomings of globals along with them while hiding behind an OO veneer. And they bring
their own problems to the table, as well.
I would disagree. With a singleton you have much greater control over the object than with a regular global variable. A global variable is created before the main() function is executed and is destroyed after main() returns which can cause many problems (try declaring a global pixmap in Qt). With singleton this is not the case. Furthermore it lets you safely use C++ paradigms such as inheritance. The article you mention is also irrelevant here - it describes use of the singleton pattern against passing data as arguments, not against using global variables. All solutions have their pros and cons, you can't say "singletons are bad" just because they don't solve a particular problem.
Re: How to make all-files-global variable?
Quote:
Originally Posted by
wysota
Hmm?? Did you follow the link I gave you?
Ofcourse, but I know how to create a global variable! The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
Re: How to make all-files-global variable?
Quote:
Originally Posted by
hakermania
Ofcourse, but I know how to create a global variable! The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
What's the difference between "global" and "fully global"? :) Do you know the "extern" keyword from C?
Re: How to make all-files-global variable?
Quote:
Originally Posted by
wysota
Do you know the "extern" keyword from C?
...and that is perfectly described at the above mentioned site: http://www.learncpp.com/cpp-tutorial...bal-variables/.
Re: How to make all-files-global variable?
Quote:
Originally Posted by
hakermania
The problem is that I want to has access to this variable from all the .cpp files I use (as I said I have a lot of dialogs, every of which has its one .cpp file). That's why I named it "fully global" and not just global.
There's no such thing as a "fully global" variable. It's either global (Accessible by all files) or not. Maybe you are confusing global (accessible to all files in a project) and static (file-local) variables.
Re: How to make all-files-global variable?
Quote:
Do you know the "extern" keyword from C?
Do you know the "extern" keyword from asm? Sorry, but I couldn't resist. Know your roots, people!:D
Re: How to make all-files-global variable?
No, as "asm" is meaningless without a processor architecture and manufacturer.
For example, some packages used XDEF to define a symbol which could be exported, and XREF to import those same symbols.
Re: How to make all-files-global variable?
MASM/Wintel/ALL (Intel and AMD share mnemonics).
Re: How to make all-files-global variable?
That would be fine if Qt was only for Intel/AMD, but it's clearly not. Some people only use Qt on ARM platform, for example.
If you changed your wording to 'Do you know the "extern" keyword from masm?' it would make perfect sense.
Re: How to make all-files-global variable?
For anybody who follows thisthread the solution is the following:
make an new header file and include it into your project. the header file must inlude
Code:
extern int IntName;
. Then put #include "headerfile.h" into the mainwindow.cpp file, and after all the inclusion type int IntName = 1(or any other value). Then you can have check for e.g. the language at the constructor of the mainwidow.cpp and include the header file into the preferences.cpp. then, in the preferences.cpp write after the inclusion int IntName; and then you will be able to check his value right now. That's all.
Re: How to make all-files-global variable?
My post was meant to be on the comical side of things. It didn't really have anything to do with Qt (not really sure why you are referring to it). I thought a few might have had experience with MASM and would have picked up on the reference. So much for that.