-
Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.
The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.
So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?
Thanks in advance.
-
Hi
Im not sure what you are asking.Say we have
myvars.h
extern bool globalbool;and in myvars.cpp
#include "myvars.h"
bool globalbool;Then in
other.cpp file
#include "myvars.h"and you can use globalbool directly.
Note: this is not nice code/ good design.
Can I ask what the goal is ?
(moved to c++)
-
Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.
The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.
So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?
Thanks in advance.
@Konstantinos said in Boolean variable - One time declaration - definition:
Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?
yes, a that's a global static.
just create a header
global.h
and typebool myGlobalBool = true;
in it. now just `#inlcude "global.h" to use it.An important warning:
if you need such a thing your design is probably wrong
-
Hi all. I want to have a Boolean variable, which will be declared and defined, only one time.
The global variable with the "extern", is declared one time, but it should be defined many times in any class that is used.
So my question is this: Can I declare and define a Boolean variable just one time, in one header for example, and then to use it in any class I want, without more definitions?
Thanks in advance.
@VRonin said in Boolean variable - One time declaration - definition:
just create a header global.h and type bool myGlobalBool = true; in it. now just `#inlcude "global.h" to use it.
Nope, follow @mrjj's advice. Defining a variable in a header is going to give you infinite amount of "symbol redefinition" errors. Declare the variable as
extern
, define it in a.cpp
file. I agree, though, that if you need to do this you're probably doing something suspicious. -
Hi,
If u want to use boolean value which is declared and defined, which has to be used across all other class, y dont u use definition variables.
- Ex :
bool value = true;
u have declared and defined value as true, which will be required for all classes.
instead use
- If ur not changing the value dynamically , go for option definition variable.
Create the separate class and define
#define boolValue false
Or
#define boolValue trueSo in this u can use boolValue variable in all classes.
MyFirstClass.cpp
#include "common.h"
use the variable boolValue in MyFirstClassMySecondClass.cpp
#include "common.h"
use the variable boolValue in MySecondClassThanks,
- Ex :
-
Hi,
If u want to use boolean value which is declared and defined, which has to be used across all other class, y dont u use definition variables.
- Ex :
bool value = true;
u have declared and defined value as true, which will be required for all classes.
instead use
- If ur not changing the value dynamically , go for option definition variable.
Create the separate class and define
#define boolValue false
Or
#define boolValue trueSo in this u can use boolValue variable in all classes.
MyFirstClass.cpp
#include "common.h"
use the variable boolValue in MyFirstClassMySecondClass.cpp
#include "common.h"
use the variable boolValue in MySecondClassThanks,
@Pradeep-Kumar said in Boolean variable - One time declaration - definition:
#define boolValue false
this does not define a variable, it defines another name for false.
Whenever you then write boolValue the preprocessor will replace it with false.
So you can just use false.
This does not answer the original question. - Ex :
-
@Pradeep-Kumar said in Boolean variable - One time declaration - definition:
#define boolValue false
this does not define a variable, it defines another name for false.
Whenever you then write boolValue the preprocessor will replace it with false.
So you can just use false.
This does not answer the original question.Hey sorry about the misconception of the topic and my post.
i should have rephrased saying definition variables can be across all the classes.
and not as mentioned in the topic as declared and defined.Thanks,