Friday, July 4th 2008, 10:06pm UTC+1

You are not logged in.

  • Login
  • Register

1

Sunday, April 20th 2008, 4:29pm

Global variable

Hi I want to know how can I use a global variable, I used the public statement but it doesn't work.

This post has been edited 1 times, last edit by "katulu50" (Apr 20th 2008, 4:29pm)

  • Go to the top of the page

WolfMM

Beginner

Posts: 47

Location: MilkyWay::Solar System::Earth:Europe:PL

2

Sunday, April 20th 2008, 5:57pm

RE: Global variable

Perhaps you should get back to the books firts?

Hint: Use scope operator ::
Qui penis auqam turbat !?
  • Go to the top of the page

seguprasad

Trainee

Posts: 103

Location: INDIA

Occupation: Developer

3

Tuesday, April 22nd 2008, 1:49pm

Dear Katulu50,

can you send some more details, if possible with sample code.

thanks,
-Segu,
  • Go to the top of the page

WolfMM

Beginner

Posts: 47

Location: MilkyWay::Solar System::Earth:Europe:PL

4

Wednesday, April 23rd 2008, 8:00pm

What for?
He asked general question how to use global variable. It's simple; you just need to use scope operator, that's all. If he can't figure out how excacly, then he has to get back to books.

It's better to give fishing rod (hint how to solve problems) instead of fish (solution for particular problem).
Qui penis auqam turbat !?
  • Go to the top of the page

5

Wednesday, April 23rd 2008, 10:10pm

I would suggest fully reviewing this site.
http://www.cplusplus.com/doc/tutorial/
You will be beating your head against a wall if you do not understand the basics.
If you are coming from anotherprogramming language, I think this is a good quick and concise guide.
  • Go to the top of the page

seguprasad

Trainee

Posts: 103

Location: INDIA

Occupation: Developer

6

Thursday, April 24th 2008, 4:57am

Dear WolfMM,

Please don't get excited.

Generally I will use scope resolution :: for static variables only not for public variables.

That's the reason why I asked him for more details like which language, compiler and platform.

I don't know that programmers use scope resolution :: for public variables also. If you are doing like this could you please let me know some example if possible.

I am very sorry, If I hearted any one on this issue.
My intention is to solve the problem by knowing more details, so that we can solve the problems with minimum impact on other functionality.

More over, as per my knowledge, this forum is not for training.

And also this forum administrators suggest to give details about compiler, platform, etc.

thanks,
-Segu,
  • Go to the top of the page

kunalnandi

Beginner

Posts: 18

Location: India

Occupation: Programmer

7

Tuesday, April 29th 2008, 12:29pm

Hello,

Use extern to declare global variable.

Regards
Regards
Kunal Nandi
  • Go to the top of the page

8

Saturday, May 17th 2008, 6:32am

RE: Global variable

if you're just looking for a global variable, declare it before your main() and you're good to go. as somebody pointed out before, any of the online c/++ tutorials is going to help you out.
  • Go to the top of the page

24pm

Beginner

Posts: 6

Location: Russia, Kemerovo

9

Thursday, May 22nd 2008, 9:03am

There are several ways of doing this:
a) To use global variables just for this unit, it is enougth to discribe them after #include and before first procedure. For example:
# include "someheader.h"
int i;
void SomeProc()
{
i = 5;
}
b) To use variable outside this unit, do as in (a) and add to header file:
extern int i;

c) If variables are in class use static variables via scope "::"
1. In header:
class Config

{

public:

static QString imgDir;

...

}
In another unit:
#include "config.h"

...

void SomeProc()

{
QString str = Config::imgDir;

}
2. Use static representation of your class where your global variables are. This is like (c.1), but a little more complicated. Guys from Qt likes this approach, though :-) Here we make everything in class accessible as static (read global). Good for large classes with many global variables.
In header:
class Config
{
public:
Config();
static Config *configuration();
QString imgDir;
...
}

In .cpp file:
static Config *static_configuration = 0;

Config::Config()
{
if( !static_configuration ) {
static_configuration = this;
} else {
qWarning( "Multiple configurations not allowed!" );
}
}

Config *Config::configuration()
{
Q_ASSERT( static_configuration );
return static_configuration;
}

In another unit:
#include "config.h"
...
void SomeProc()
{
QString str = Config::configuration()->imgDir;
}

I hope this would be enougth to start with, but such a question is really a basic one. Theory should be studied!
Research Assistant - a tool for a researcher
  • Go to the top of the page

stinos

Intermediate

10

Thursday, May 22nd 2008, 9:16am

2 is just a really basic (not thread-safe at all, and not reusable) form of the Singleton design pattern.
madinsjamania
  • Go to the top of the page

krsmichael

Beginner

Posts: 49

Location: California

Occupation: Software Engineer

11

Monday, May 26th 2008, 6:55pm

RE: Global variable

Hi I want to know how can I use a global variable, I used the public statement but it doesn't work.
Your question isn't sequitur. The public c++ keyword enables code outside of a class to see the functions and data of a class.

i.e.

class Foo
{
public:
void TheWorldCanSeeMe(int aVariable);
int _theWorldCanSeeMeToo;

};

To create and use a global variable, which in general is not a good idea, but every once in a blue moon is most expedient, declare in a header file:

We will call this globals.h.

extern int gMyGlobalVariable;

Anything that wants to USE the variable will include the globals.h header file.

In a file named, global.cpp, you will define the variable:

int gMyGlobalVariable(0); // It is a good idea to initialize it to a known value.

So, any code that has included globals.h can access that variable. i.e.

gMyGlobalVariable = 3;

You can make classes global as well. Again, make certain you need to. There is ALOT of penalty for choosing a global variable.

In the cpp
  • Go to the top of the page

Rate this thread