PDA

View Full Version : How to make all-files-global variable?



hakermania
9th December 2010, 20:54
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.


//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

Lykurg
9th December 2010, 21:06
If it is only for translation you should have a look at Qt internationalization functions. Otherwise have a look at Singleton Pattern.

EDIT: Or use global functions/variables... But I am fine with the singleton pattern:D

hakermania
9th December 2010, 21:22
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?

Lykurg
9th December 2010, 21:32
Well there are different approaches one is described here: http://www.learncpp.com/cpp-tutorial/42-global-variables/. Or you can use global functions in a separate file with static variables and then do something like
int languageCode()
{
static int code = -1;
if (-1 == code)
{
// calculate code
}
return code;
} so the calculation will also only be done once.

wysota
9th December 2010, 21:35
There is no concept of "fully global" variable. The variable is either global or not.

http://www.cplusplus.com/doc/tutorial/variables/

hakermania
9th December 2010, 21:53
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

wysota
9th December 2010, 22:24
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?

squidge
10th December 2010, 00:19
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.

SixDegrees
10th December 2010, 08:06
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 (http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx) to the table, as well.

wysota
10th December 2010, 08:28
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 (http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx) 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.

hakermania
10th December 2010, 13:04
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.

wysota
10th December 2010, 13:40
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?

Lykurg
10th December 2010, 14:09
Do you know the "extern" keyword from C?...and that is perfectly described at the above mentioned site: http://www.learncpp.com/cpp-tutorial/42-global-variables/.

squidge
10th December 2010, 14:24
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.

Timoteo
11th December 2010, 10:32
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

squidge
11th December 2010, 10:49
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.

Timoteo
11th December 2010, 10:56
MASM/Wintel/ALL (Intel and AMD share mnemonics).

squidge
11th December 2010, 11:28
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.

hakermania
11th December 2010, 11:31
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
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.

Timoteo
11th December 2010, 11:46
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.

squidge
11th December 2010, 12:09
I was referring to Qt because this is a forum which talks about Qt as the main point of interest :)

Anyway, enough off-topic, hopefully the OP now has what he wants.

wysota
11th December 2010, 12:25
Do you know the "extern" keyword from asm? Sorry, but I couldn't resist. Know your roots, people!:D

My roots is Atari BASIC, there is no "extern" keyword there ;)

Timoteo
11th December 2010, 12:30
Hah, my BASIC was the Commodore flavor. How old are you?:confused:

Atari had a cartridge based assembler didn't it? Crazy stuff.:D

wysota
11th December 2010, 12:41
How old are you?:confused:
Old enough. I started programming when I was around 9 yo.


Atari had a cartridge based assembler didn't it?
Cartridge is just a storage device so I guess this is not some important feature of it. I remember one could do assembly directly from BASIC by providing some weird blocks of numbers. At the time I didn't understand any of it.

SixDegrees
11th December 2010, 12:46
The C64 used a cartridge-based asssembler. Their cartridge was a read-only expansion device, more often used to ship games, but in this case it gave you an editor and an actual assembly language implementation so you could program using HLL tokens which the assembler then converted into machine code.

The first C64 program I wrote in assembler was a rewrite of a Commodore Basic version of the mathematical game "Life". It ran over a thousand times faster than it's interpreted cousin.

Timoteo
11th December 2010, 12:53
Man, I was like..10 i think at the time...and I didn't have access to anything but a VIC-20 and the tape deck and some programming magazines. It is weird that I remember (wrongly) Atari having a cartridge assembler and not the C64. Did the VIC-20 have it also? I feel really deprived now.

wysota
11th December 2010, 12:58
I just checked in wikipedia. Earlier versions of Atari had BASIC provided on a cartridge while later ones (like my Atari 65 XE) had it built-in into ROM. Nothing about assembly though.

marcvanriet
11th December 2010, 13:03
The C64 cartridge based assembler was the red 'Power cartridge' guess ? Then you could also use your function keys to show a directory listing without overwriting your program (as you would do with "load *,8,1" if I remember correctly). Great tool.

Regards,
Marc
P.S. I was 13 at the time ;)

squidge
11th December 2010, 13:04
I remember writing machine level code for the VIC-20 and C64 using DATA statements along with POKE commands. I had to write all the assembler on scrapes of paper, manually convert it into numbers for the DATA statements, and then watch it crash when I run it.

My first assembler was for the Commodore Amiga, called ASM-One (freeware) and then changed to DevPac (commercial).

Sheesh, I feel like I'm showing my age :o

Timoteo
11th December 2010, 13:12
Ahh, don't feel bad. :) Yes, I remember PEEK and POKE and flipping through the programming magazines to find the encoding for "cool looking" sprites. Graph paper was a must if you were sketching out your own.

wysota
11th December 2010, 13:43
I remember writing machine level code for the VIC-20 and C64 using DATA statements along with POKE commands. I had to write all the assembler on scrapes of paper, manually convert it into numbers for the DATA statements, and then watch it crash when I run it.
That's the part I didn't understand at the time.


My first assembler was for the Commodore Amiga, called ASM-One (freeware)
Been there, done that. I remember trying to battle with the Intuition library to be able to open a window or something :)

squidge
11th December 2010, 14:00
I remember playing with the builtin libraries and coming up with the verdict that the libraries were far too slow for what I wanted to do. I then got a copy of the Amiga hardware reference manual and had much more fun. I'm sure most Amiga nuts will remember bouncing copper bars, which was always the first hardware hackers demo, followed by spinning 3D floppy disks and the like, those always seemed to be the first demos by people getting into assembler development. I also remember writing a track loader as I wasn't impressed with the standard OS disk routines (and the famous 0×4489 disk sync).

nroberts
29th December 2010, 18:05
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

This being 2010, many decades of studying design techniques and measuring the success of the projects that use them...people have pretty much universally decided that global variables are to be avoided at nearly all cost.

Actually, people knew this a long time ago. Most people discover it on their own if they don't learn from those ahead of them in the field.

You might have a case for using one. I'd probably look for a different way but I'm not working on your project so...wtf do I know. At any rate, there's no reason to go through the incredibly expensive effort of altering a language for something like this. Declaring and defining global variables is already fairly easy, though it could be admitted that someone brand new could find it unintuitive.

I followed the link you were given, didn't see what you need to know there. Here's how to do it:

In a header file:


extern type my_var;


This is a declaration.

In ONE .cpp file:


type my_var = a_value;


This is a definition AND an initialization. I suggest initializing your variable, but if you don't it will be "default initialized" (set to 0 in case of an int). You could also initialize through a function call, and in fact I use this a LOT for factory registration (a bigger topic). Just remember that anything you use to initialize your variable will happen before main is called and you have *absolutely no way of knowing in which order* except within specific cpp files.

Once you've done the above two things you can use that variable anywhere that includes the .h you declared it in.

Technically you don't have to have a header, but the reasons behind that would just confuse the hell out of you. Just do it this way...if you really must use a global variable.