What is the difference between variables declared in the header file vs variable declared in the source file

Hi,

Two quick questions.

1- What would be difference between the folloing varable declarations?

2- Are both of these variables considered global variable to the class (class scope)?

Declaring vars in the source file
1
2
3
4
5
6
7
8
#includes "here"

int myVar;

SomeClass::SomeClass()
{
   // constructor code
}


Declaring vars in the header file

1
2
3
4
5
6
7
#ifndef MAINWINDOW_H
#define MAINWINDOW_Hprivate:
	int myVar;
...
#endif // MAINWINDOW_H  


Thanks
Your source file is declaring a global variable and your header file appears to be declaring a private member in a class/struct. The two declarations are completely different things in this case.
Ok thank you for your reply.

The main reason for the question is because I'm trying to avoid using global variables (first example) and I wasn't sure if declaring a "private" member in the header file would be considered global variable to the class.

Is my second example considered a global variable to the class since it is visible to all functions, BUT private to external classes?

Thanks

Why are you trying to avoid using globals?
And while I'm at it, why do you need global behaviour?

If you're worried about naming conflicts use a namespace or a long name.

If you're worried about the user not giving enough care to the global but you're forced to use a global anyway give it a prefix like 'g_' to force the programmer to know it's global and maybe think harder about how to use it.

EDIT: I mean, it wouldn't be the worst thing to define a class to wrap some globals and stick some static public members in there, but it kind of feels like you're just making a fake namespace.

EDIT EDIT: I think I understand what you're asking now, and yes they are totally different. The first will make a single global variable, and the second will create a member variable local to every object created from the class. Each variable, however, can be accessed the same from inside the class; by the name. Even so, they are being stored very differently.

If you want a 'private' single variable attached to the CLASS and not to EACH OBJECT, then you want a private scope STATIC member variable in the class, which can still be accessed in the class scope by its name but nowhere else.
Last edited on
hi,
"private" does not do what you think in this case. the word "private" applies when declaring class members, and not anywhere in the code. so it just does nothing.

the lines from header file are just "copy/pasted" by the compiler where you include it, so there is no difference between this:
header.h
1
2
3
class Test{
    int number.
};
main.cpp
1
2
3
4
5
#include "header.h"
int main(){
    Test test;
    return 0;
}


and this:
main.cpp
1
2
3
4
5
6
7
8
class Test{
    int number.
};

int main(){
    Test test;
    return 0;
}
Consider the following example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int var = 10;

namespace N1
{
   int var = 20;
}

struct A { int var = 30; ]
struct B { static int var; }

int B::var = 40;

int f()
{
   int var = 50;
   return var;
}

int main()
{
   int var = 60;

   std::cout << "::var = " << ::var << std::endl;
   std::cout << "N1::var = " << N1::var << std::endl;
   std::cout << "A::var = " << A().var << std::endl;
   std::cout << "B::var = " << B::var << std::endl;
   std::cout << "in f() var = " << f() << std::endl;
   std::cout << "var = " << var << std::endl;
}
Hi thank you all for the good information.

@Veltas

Why are you trying to avoid using globals?


The only reason I wan to avoid using global variables is because I have heard and being adviced by a teacher to not use global variables since they can be changed from any part of your program and one can eventually loose track of where the changes occur, other than that I don't have a good reason.



And while I'm at it, why do you need global behaviour?


Well I guess because parts of my program need to be accessed from multiple functions. I know I know I need to start using getters and setters or as you suggested static variables.

Thank you all very much.
Last edited on
The only reason I wan to avoid using global variables is because I have heard and being adviced by a teacher to not use global variables since they can be changed from any part of your program and one can eventually loose track of where the changes occur, other than that I don't have a good reason.

There are several good reasons to avoid globals, and that's just one of them. Using globals is a bad habit to get into, so well done for trying to do it right :)


Topic archived. No new replies allowed.