Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How do you define a UDT (struct) as global?

Author
Message
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 24th Feb 2008 19:48
In DBP it is easy:


The equivelant code in C++ (as far as I can tell):


Now, the book that I'm trying to learn C++ from (The Complete Idiots Guide To C++) tells me that any variables that are declared before Main() will be global (external) but if I put that bit of code at the top the variables are still not global.

What's going on?

Thanks



Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 24th Feb 2008 19:56 Edited at: 24th Feb 2008 20:02
Don't use the struct keyword in the declaration of a variable. And don't forget semicolons at the end of each statement!

Any variable declared outside of a function is in the global scope. Be aware that it must appear in the source file before any function that accesses it. If you need to access it from multiple source files, define it in one (preferably above all functions in that file) then declare it in other files with the extern keyword to indicate that the variable is defined in another source file. Usually you do this in header files, and then include the header file into the source file that you want to access this from.

Example:

Main source file:


Other source files/headers:


Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 24th Feb 2008 20:04
The missing semicolons was just a posting error

As for the use of the struct keyword in the declaration of the variable. That is what it says in the book but I removed it and you are correct ... it works!

Books, pah!

Thanks



Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 24th Feb 2008 20:10
You're welcome.

Quote: "As for the use of the struct keyword in the declaration of the variable. That is what it says in the book but I removed it and you are correct ... it works!"

I've seen that in a lot of examples online. The only advantage I can see is that it allows you to declare a variable with the same name as the structure, but I don't see why anyone would want to introduce that ambiguity into their code.

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 25th Feb 2008 03:24
Namespaces are something else you should look into on this matter.

Login to post a reply

Server time is: 2024-11-17 06:57:29
Your offset time is: 2024-11-17 06:57:29