Using global instance of a class by another global variable in other file

uchar

I have these files (simplified version)

a.h

class A
{

public:
     A(int){}

int operator [](int a){return a;}
};

A a(2);

main.cpp

#include<a.h>

class B
{
public:
    B(int){}
};

B b(a[2]);

int main()
{
  //use b here...
}

Q: Can I use the above code ? Is it correct ?!

Candlemancer

The code above (almost) compiles, but it probably doesn't do what you'd expect. A couple issues . . .

  1. #include<a.h> should probably be #include "a.h" if a.h is located in the same directory as main.cpp.

  2. B b(a[2]) might create a new instance of class A, instead of using the one you defined in a.h. Add extern A a; before you use a to tell your compiler that you want to use the a declared in another file.

Finally, depending on your application sometimes global variables can be frowned upon. If it's appropriate, consider using encapsulation instead of global variables. Something like

Class B {

public:
    B(int i){}

private:
    A a(); // Or extern A a;
};

Would work if you only needed to access a from b.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using a global variable with a thread

From Dev

Declaring Global variable in Python Class

From Dev

Global class instance counting (with Semaphores)

From Dev

calling class object from other class Not using "global" php

From Dev

Can a function access the instance of a class without using global in python?

From Dev

"Embed" a global variable in javascript file using grunt

From Dev

Global instance of a class or static class with initialization method

From Dev

c++ program crash when creating global instance of class whose constructor references a global variable

From Dev

Using global string script variable in Run or other section in Inno Setup

From Dev

View to global class variable?

From Dev

Kotlin Set Global Variable in Global Class

From Dev

Make global instance of class - JAVA

From Dev

Calling a variable from a global class

From Dev

calling class object from other class Not using "global" php

From Dev

PHP Class get Global Variable

From Dev

Stored procedure in class and global variable

From Dev

How can a Router talk to a View other than using a global variable?

From Dev

PHP - Using a global variable inside a class (Fatal Error)

From Dev

global variable in class php

From Dev

Create Multiple instance of c++ source file with global variable From another C++ Class

From Dev

Swift how to make a variable global in a file, but not share to other files

From Dev

Using global variable in function

From Dev

Using global string script variable in Run or other section in Inno Setup

From Dev

Using getline with a global variable

From Dev

Inner class with global variable

From Dev

function call inside another function with using javascript Global variable

From Dev

php - use class methods in another class using global

From Dev

Global variable (Class)

From Dev

Accessing a global variable defined in a .cpp file in another .cpp file

Related Related

  1. 1

    Using a global variable with a thread

  2. 2

    Declaring Global variable in Python Class

  3. 3

    Global class instance counting (with Semaphores)

  4. 4

    calling class object from other class Not using "global" php

  5. 5

    Can a function access the instance of a class without using global in python?

  6. 6

    "Embed" a global variable in javascript file using grunt

  7. 7

    Global instance of a class or static class with initialization method

  8. 8

    c++ program crash when creating global instance of class whose constructor references a global variable

  9. 9

    Using global string script variable in Run or other section in Inno Setup

  10. 10

    View to global class variable?

  11. 11

    Kotlin Set Global Variable in Global Class

  12. 12

    Make global instance of class - JAVA

  13. 13

    Calling a variable from a global class

  14. 14

    calling class object from other class Not using "global" php

  15. 15

    PHP Class get Global Variable

  16. 16

    Stored procedure in class and global variable

  17. 17

    How can a Router talk to a View other than using a global variable?

  18. 18

    PHP - Using a global variable inside a class (Fatal Error)

  19. 19

    global variable in class php

  20. 20

    Create Multiple instance of c++ source file with global variable From another C++ Class

  21. 21

    Swift how to make a variable global in a file, but not share to other files

  22. 22

    Using global variable in function

  23. 23

    Using global string script variable in Run or other section in Inno Setup

  24. 24

    Using getline with a global variable

  25. 25

    Inner class with global variable

  26. 26

    function call inside another function with using javascript Global variable

  27. 27

    php - use class methods in another class using global

  28. 28

    Global variable (Class)

  29. 29

    Accessing a global variable defined in a .cpp file in another .cpp file

HotTag

Archive