Qt C++ Create a Global Variable accessible to all classes - c++

Is it possible to create a variable globally accessible to all classes in my program in which if I change it's value from one class it will also change for all other classes?
If so, how do I implement such?

Everyone keeps parroting that "globals are evil", but I say that pretty much everything can be used or misused. Also, if globals were intrinsically bad, they simply wouldn't be allowed in the first place. In the case of globals, there is quite the potential for accidental misuse to very negative consequences, but they are 100% fine if you actually know what you are doing:
// globals.h - for including globals
extern int globalInt; // note the "extern" part
// globals.cpp - implementing the globals
int globalInt = 667;
It may also be a very good idea to use namespaces to avoid naming conflicts and keep the scope cleaner, unless you are particularly meticulous with your naming conventions, which is the olden "C" way to do namespaces.
Also, and especially if you are using multiple threads, it might be a good idea to create an interface for accessing the encapsulated global stuff that will also have modular locks or even be lock-less wherever possible (for example direct use of atomics).
But a global is not necessarily the only solution. Depending on what you actually need, singletons or static class members might do the trick while keeping it a little more tidy and avoiding the use of evil globals.

Such variable have to be defined once somewhere but then used in different compilation units. However, in order to use something, you know, you need to tell the compiler that it exists (declare it). extern keyword lets you declare that something exists somewhere else.
In order to structure your code you can do what xkenshin14x (kind of?) proposed:
global.h
#pragma once
#include <string>
// Declaration
namespace global
{
extern int i;
extern float f;
extern ::std::string s;
}
global.cpp
#include "global.h"
// Definition
namespace global
{
int i = 100;
float f = 20.0f;
::std::string s = "string";
}
main.cpp
#include "global.h"
int main(int argc, char *argv[])
{
std::cout << global::i << " " << global::f << " " << global::s;
return 0;
}
It is good to use a namespace in this case as it lets you to avoid name collisions which are inherent for global variables.
Alternatively, it is possible to encapsulate all global stuff inside a one "global" object. I quoted word "global" because indeed this object is static to a global function so technically no global variables involved :)
Here is a header only implementation:
global.h
#pragma once
class Global
{
public:
int i = 100;
float f = 20.0f;
// and other "global" variable
public:
Global() = default;
Global(const Global&) = delete;
Global(Global&&) = delete;
static Global& Instance()
{
static Global global;
return global;
}
};
namespace {
Global& global = Global::Instance();
}
// static Global& global = Global::Instance(); if you like
test.h
#pragma once
#include "global.h"
struct Test
{
void ChangeGlobal() {
global.i++;
}
};
main.cpp
#include <iostream>
#include "global.h"
#include "test.h"
int main()
{
Test t;
std::cout << global.i << " " << global.f << std::endl;
t.ChangeGlobal();
std::cout << global.i << " " << global.f << std::endl;
return 0;
}
There are at least two benefits in a such approach:
You don't literally use any global objects.
In your Global class you can add variable accessors with mutexes inside where it is needed. For example, void SetSomething(const Something& something) { ... }

If you just use this global variable in UI thread(for QApplication ui app) or main thread(for QCoreApplication console app),it will be convenient to code and you may design a custom data struct.But if you use it in multithread environment,you should need mutex or atomic to protect global variables.

Global variables should be avoided as you probably know.
But you could create a header file for the global variables and #include "globals.h" in each place you want to use it in. Then you can use each variable normally.

Related

Registering file handlers in code running prior to main()

I'm looking into ways to prevent unnecessary clutter in setup code in main() as well as various other places. I often have tons of setup code that registers itself with some factory. A standard example is e.g. handlers for various file types.
To avoid having to write this code and instead just make handlers magically work if linked into the application, I figured I could replace the code by something like the following:
test.cc:
int main() {
return 0;
}
loader.h:
#ifndef LOADER_H_
#define LOADER_H_
#include <functional>
namespace loader {
class Loader {
public:
Loader(std::function<void()> f);
};
} // namespace loader
#define REGISTER_HANDLER(name, f) \
namespace { \
::loader::Loader _macro_internal_ ## name(f); \
}
#endif // LOADER_H_
loader.cc:
#include "loader.h"
#include <iostream>
namespace loader {
Loader::Loader(std::function<void()> f) { f(); }
} // namespace loader
a.cc:
#include <iostream>
#include "loader.h"
REGISTER_HANDLER(a, []() {
std::cout << "hello from a" << std::endl;
})
The idea here is that a.cc would in a real application e.g. call some method where it registers it self as a handler for a certain file type. Compiling the code with c++ -std=c++11 test.cc loader.cc a.cc creates a binary that prints "hello from a" while c++ -std=c++11 test.cc loader.cc stays silent.
I'm wondering if there's something subtle that I might need to be careful with? For example, if someone creates complex objects in the lambda that is run here, I assume weird things can happen during cleanup for example in a multithreaded application?
You wrote:
... unnecessary clutter in setup code in main() ...
int main() {
return 0;
}
This is not preventing unnecessary clutter. This is hiding your initializations. They still occur, but now you have to chase after them. That's really not the way to do it. Also, it will force the use of a lot of global state - in many independent global variables, most probably - which is also a bad thing. Instead, consider writing something like:
class my_app_state { /* ... */ };
my_app_state initialize(/* perhaps with argc and argv here? */) {
//
// Your "unnecessary" clutter goes here...
//
return whatever;
}
int main() {
auto app_state = initialize();
//
// do stuff involving the app_state...
//
}
and don't try to "game" the program loader.
This approach is not guaranteed to work:
[basic.start.dynamic]/4 It is implementation-defined whether the dynamic initialization of a non-local non-inline variable with static storage duration is sequenced before the first statement of main or is deferred. If it is deferred, it strongly happens before any non-initialization odr-use of any non-inline function or non-inline variable defined in the same translation unit as the variable to be initialized.
Thus, the initialization of _macro_internal_a may be deferred until something in a.cc is used. And since nothing in a.cc is in fact used, the initialization may not be performed at all.
In practice, linkers tend to discard object files that do not appear to be referenced by anything in the program (especially when those files come from libraries).

How to make a variable available to multiple .cpp files using a class?

This question has derived from this one.
I have a working program which must be split into multiple parts. In this program is needed to use a variable (now it's a GTK+ one :P) many times in parts of the program that will end up in separated .cpp files.
So, I made a simple example to understand how to make variables available to the program parts. A modified version of the previous code would be:
#include <iostream>
using namespace std;
int entero = 10;
void function()
{
cout<<entero<<endl;
//action1...;
}
void separated_function()
{
cout<<entero<<endl;
//action2...;
}
int main( int argc, char *argv[] )
{
function();
separated_function();
cout<<entero<<endl;
//something else with the mentioned variables...;
return 0;
}
It is needed to split the code correctly, to have function(), another_function() and main() in separated .cpp files,and make entero avaliable to all of them... BUT:
In the previous question #NeilKirk commented:Do not use global variables. Put the required state into a struct or class, and pass it to functions as necessary as a parameter (And I also have found many web pages pointing that is not recommended to use global variables).
And, as far I can understand, in the answer provided by #PaulH., he is describing how to make variables avaliable by making them global.
This answer was very useful, it worked fine not only with char arrays, but also with ints, strings and GTK+ variables (or pointers to variables :P).
But since this method is not recommended, I would thank anyone who could show what would be the correct way to split the code passing the variables as a function parameter or some other method more recommended than the - working - global variables one.
I researched about parameters and classes, but I'm a newbie, and I messed the code up with no good result.
You need to give the parameter as a reference if you want the same comportement as a global variable
#include <iostream>
using namespace std;
// renamed the parameter to avoid confusion ('entero' is valid though)
void function(int &ent)
{
cout<<ent<<endl;
++ent; // modify its value
//action1...;
}
void separated_function(int &ent)
{
cout<<ent<<endl;
++ent; // modify its value again
//action2...;
}
int main( int argc, char *argv[] )
{
int entero = 10; // initializing the variable
// give the parameter by reference => the functions will be able to modify its value
function(entero);
separated_function(entero);
cout<<entero<<endl;
//something else with the mentioned variables...;
return 0;
}
output:
10
11
12
Defining a class or struct in a header file is the way to go, then include the header file in all source files that needs the classes or structures. You can also place function prototypes or preprocessor macros in header files if they are needed by multiple source files, as well as variable declarations (e.g. extern int some_int_var;) and namespace declarations.
You will not get multiple definition errors from defining the classes, because classes is a concept for the compiler to handle, classes themselves are never passed on for the linker where multiple definition errors occurs.
Lets take a simple example, with one header file and two source files.
First the header file, e.g. myheader.h:
#ifndef MYHEADER_H
#define MYHEADER_H
// The above is called include guards (https://en.wikipedia.org/wiki/Include_guard)
// and are used to protect the header file from being included
// by the same source file twice
// Define a namespace
namespace foo
{
// Define a class
class my_class
{
public:
my_class(int val)
: value_(val)
{}
int get_value() const
{
return value_;
}
void set_value(const int val)
{
value_ = val;
}
private:
int value_;
};
// Declare a function prototype
void bar(my_class& v);
}
#endif // MYHEADER_H
The above header file defines a namespace foo and in the namespace a class my_class and a function bar.
(The namespace is strictly not necessary for a simple program like this, but for larger projects it becomes more needed.)
Then the first source file, e.g. main.cpp:
#include <iostream>
#include "myheader.h" // Include our own header file
int main()
{
using namespace foo;
my_class my_object(123); // Create an instance of the class
bar(my_object); // Call the function
std::cout << "In main(), value is " << my_object.get_value() << '\n';
// All done
}
And finally the second source file, e.g. bar.cpp:
#include <iostream>
#include "myheader.h"
void foo::bar(foo::my_class& val)
{
std::cout << "In foo::bar(), value is " << val.get_value() << '\n';
val.set_value(456);
}
Put all three files in the same project, and build. You should now get an executable program that outputs
In foo::bar(), value is 123
In main(), value is 456
I prefer to provide a functional interface to global data.
.h file:
extern int get_entero();
extern void set_entero(int v);
.cpp file:
static int entero = 10;
int get_entero()
{
return entero;
}
void set_entero(int v)
{
entero = v;
}
Then, everywhere else, use those functions.
#include "the_h_file"
void function()
{
cout << get_entero() << endl;
//action1...;
}
void separated_function()
{
cout << get_entero() << endl;
//action2...;
}
int main( int argc, char *argv[] )
{
function();
separated_function();
cout<< get_entero() <<endl;
//something else with the mentioned variables...;
return 0;
}
If you do not plan to modify the variable, it is generally ok to make it global. However, it is best to declare it with the const keyword to signal the compiler that it should not be modified, like so:
const int ENTERO = 10;
If you are using multiple cpp files, also consider using a header file for your structures and function declarations.
If you are planning on modifying the variable, just pass it around in function parameters.

[C++]Variables cross .cpp files

This is a silly question with something that must be an easy answer, but after hours of searching I cannot find the answer. What I need to do is have a pair of .cpp files, say main.cpp and help.cpp that have a variable, vars1 that they share and can both change the value and detect when that value has been changed. The way that would make sense to me is that I would simply declare the variable in a class inside a header file and include that header file in both .cpp files, but that doesn't seem to work.
Here is a copy of my code:
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "variables1.h"
using namespace std;
int main(){
variables1 vars1;
do {
cout << "Welcome\n If you need help, type 'yes' now\n";
cin.getline(vars1.input, 1024);
if (strcmp(vars1.input, "yes") == 0 || strcmp(vars1.input, "Yes") == 0){
vars1.helpvar = true;
cin.get();
}
else{
cout << "Okay then, glad that you know your way around\n";
}
cin.clear();
cout << "What would you like to do?\n";
cin.getline(vars1.input, 1024);
if (strcmp(vars1.input, "logon" ) == 0 ) {
}
} while (0 == 0);
}
help.cpp:
#include <iostream>
#include "variables1.h"
using namespace std;
int help(){
variables1 vars1;
do {
if (vars1.helpvar == true)
cout << "detecting";
} while (0 == 0);
}
variables1.h:
class variables1
{
public:
bool helpvar;
char input[1024];
};
Actually what you are doing is that for the main file and the help.cpp you are creating two different objects and are setting the helpvar variable for each of them separately. What you want is to have a single object that is used by both help.cpp and main to only modify a single instance of the helpvar variable.
Change your help function to be along the lines of
int help(const variables1& myHelpobject ){
if (myHelpobject.helpvar == true) {
cout << "detecting";
}
}
and then call the function in main as:
help(vars1)
What you were doing before was creating a separate, independent, help object.
Here we are creating the object in main and then passing a reference to it to the function.
The technique to use depends on the purpose of your variable.
If it is some sort of global parameters, that you have to use throughout all your code, the simplest is to define it as a global variable:
main file:
variables1 vars1; // outside all functions
int main(){
...
}
Either in variables1.h or in the other cpp files using the variable:
extern variables1 vars1; //outside all functions
However the code to initialise and maintain these variables in a should also be defined in the class. The constructor shall for example define the values by default, such as if help is enable or disabled.
If your variables are for communicating between different parts of your code, and especially if the main goal of some code is to process the content of these variables, then should better make this clear by passing the variable as parameter (by reference (&) if the communication is bidirectional, or by value).
There are 2 main issues with the code as posted:
int help() is never run
Something needs to call this function for it to run. There isn't anything doing that so regardless of the value of vars1.helpvar you are never going to see "detecting" output.
Consider adding a help.hpp with the definition of the function and call the function from main.
vars1.helpvar is not shared between main and int help()
Currently you have two instances of variables1 and helpvar is a member variable so each instance has a separate copy.
You could either:
Make helpvar a static member of variables1
Share once instance of variables1 between both main and help.
The use of static variables is more likely give design problem later so I'd favour option 2.

c++ namespace with class

ns.cpp:
#include <iostream>
namespace ns { // want to access this globally
class A ;
}
class ns::A {
public:
int x1;
char s1[128];
};
int main()
{
int doit();
//using namespace ns;
ns::A a;
a.x1= 2;
std::cout << "pre " << a.x1 << "\n" ;
doit();
std::cout << "post " << a.x1 << "\n" ;
}
ns_call.cpp:
namespace ns {
class A;
}
class ns::A {
public:
int x1;
char s1[];
};
using namespace ns;
int
doit()
{
extern ns::A a;
a.x1= 100;
}
in ns.cpp, a class is declard within a namespace.
the class is defined followed.
Variables in the class are to be accessed globally. This
is the goal of the namespace.
ns_call.cpp then access 1 of the class' member, x1.
The 2 files were compiled OK with gcc 5.4.1 in Fedora 14.
run output is:
pre 2
post 2
I expected 'post 100' because i wanted to access int x1 in class A
globally.
None of extern, namespaces or forward declarations seem to mean what you think they mean.
If you want to introduce a name that can be accessed my multiple translation units (what you seem to mean when you say "globally"), you put those definitions in a header file, and #include that header from wherever you want to use it.
What you're actually doing is introducing class A over and over again in every translation unit (eg, in each CPP file). This is a violation of the ODR at best.
I'm just gonna help you fix your problem, while you absolutely have to heed the other answers about ODR (One Definition Rule) and fix your design.
in the ns.cpp file, you have to move the line ns::A a; out of the main() function. Put it at the file scope (e.g. right before main.) Also, in the ns_call.cpp file, move the line extern ns::A a; out of the function too.
Note: you may or may not have to do the second part, and this whole method might or might not work. I don't have access to a compiler right now.
Again I have to agree with other comments that this design is flawed and it will cause you headaches.

Accessing variables outside the program in C++

id.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
static int a;
};
int A::a=20;
class b
{
public:
b()
{
cout<<A::a<<endl;
}
};
int main()
{
b *b1 = new b();
return 0;
}
id1.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
class c
{
public:
int get()
{
cout<<A::a<<endl;
}
};
int main()
{
c c1;
c1.get();
return 0;
}
This is the way they have declared and got the output in one program but when I'm trying it I'm getting errors as the class is not a namespace or the program id is not included in the id1 file... How to get the variable that is stored in one file into the other file without using namespace and including the header file is there any option for it?
Two separate programs as shown (they're separate because they both define main()) cannot share variables in any simple way.
If the two separate files were to be integrated into a single program, so one of the main() programs was replaced, then you would fall back on the standard techniques of declaring the variable A::a in a header and using that header in both modules. The header would also define class A. This is the only sane way to do it.
You could write the definition of the class twice, once in each file, and declare the variable as an extern in one file and define it in the other, but that is not particularly sensible even in this simple case and rapidly degenerates into unmaintainable disaster if the code gets any more complex and there are more shared variables.
Of course, you might want to consider not using a global variable at all, but provide instead an accessor function. However, you still end up with a header declaring the services provided by the class A and the code implementing those services, and the code consuming those services.