Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Declare a global class and access it from other classes?

Tags:

People also ask

How do you define a global class?

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.

How do you call a class variable from another class in C++?

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.

How do you extern an object in C++?

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.

What are local classes in C++?

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();
}