Global class=> Global class is a class that is defined outside of any function and it can be accessed everywhere of the program. Local class=> Local class is a class that is defined inside of any function and can be accessed only in the body of that function. It can not be accessed by the outside of the function.
If you are in a single-threaded environment, and you just want to get a value out of a class, use a getter for that value.
The extern storage class in C++The extern storage class specifier lets you declare objects that several source files can use. An extern declaration makes the described variable usable by the succeeding part of the current source file. This declaration does not replace the definition.
A local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.
I have a class which should be declared globally from main() and accessed from other declared classes in the program, how do I do that?
class A{
int i;
int value(){ return i;}
};
class B{
global A a; //or extern??
int calc(){
return a.value()+10;
}
}
main(){
global A a;
B b;
cout<<b.calc();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With