Using extern to specify global variables

I want to be able to define variables (eventually arrays) that can be accessed with the same variable name in functions in different class. So the following is a simple study using XCode and OF with two simple classes. It builds successfully and runs but it does not recognize the variables in the “display” output. So the code works but the methods in the two classes do not identify the variables defined in ofApp.

I tried to follow some other forum discussions on this issue and use their recommendations but no luck. It is due to my lack of experience in C++ memory management but hopefully someone can easily point out my problem.

Note: the only difference between “aClass1” and “aClass2” is the inclusion of the extern statement in thew “aClass1” .hpp file - and that did not influence the result.

********************************
// ofApp.h

#pragma once

#include "ofMain.h"
#include <stdio.h>
#include "aClass1.hpp"
#include "aClass2.hpp"

extern int varA1, varA2, varB1, varB2;

class ofApp : public ofBaseApp{

public:
    void setup();
    void draw();
    
public:
    aClass1 aclass1;
    aClass2 aclass2;

    int varA1, varA2, varB1, varB2;
};

// ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    varA1 = 10;
    varA2 = 11;
    varB1 = 20;
    varB2 = 21;
}

//--------------------------------------------------------------
void ofApp::draw(){
    aclass1.display();
    aclass2.display();
}``` 


//  aClass1.hpp

#pragma once

#include <stdio.h>
#include "ofMain.h"
extern int varA1, varA2, varB1, varB2;

class aClass1{

public:
    aClass1();
              
    void setup();
    void display();
    
    int varA1, varA2, varB1, varB2;
};

//  aClass1.cpp

#include "aClass1.hpp"

aClass1::aClass1(){
}
    void aClass1::setup(){        
        
    }
    
void aClass1::display(){
    cout << "in aClass1: varA1 = "<< varA1 << endl;
        
    }

//  aClass2.hpp

#pragma once

#include <stdio.h>
#include "ofMain.h"

class aClass2{
    
public:
    aClass2();
    
    void setup();
    void display();
    
    int varA1, varA2, varB1, varB2;
};

//  aClass2.cpp

#include "aClass2.hpp"

aClass2::aClass2(){
}
void aClass2::setup(){
    
}

void aClass2::display(){
    cout << "in aClass2: varB1 = "<< varB1 << endl;
}

Hey @retprof,

An alternative to using global variables might be inheritance.

In the code you posted, it seems like you’re mixing global variables and class variables together. You should be able to define a global variable once (in a source file), and then access it in any class where it is declared as extern and that includes the file where it is defined. So, try something like this:

// testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H

class testClass
{
public:
    testClass();
};

#endif // TESTCLASS_H

// testclass.cpp
#include "testclass.h"

int globalInteger = 100;
testClass::testClass() {}

// in ofApp.h
#include "testclass.h" // include where the global is defined
// before ofApp class
extern int globalInteger;

// use it in ofApp::setup()
globalInteger += 1;
ofLogNotice("globalInteger:" + to_string(globalInteger));

There is a nice explanation here: variable declaration - When to use extern in C++ - Stack Overflow

ofBook has a nice chapter on classes here: ofBook - Ooops! = Object Oriented Programming + Classes.

Generally, I think the use of global variables is discouraged in c++ because of its object orientation, and the ideas of encapsulation and inheritance in the language. A class can have public members (variables and functions) that can be accessed from outside the class. A class can also be a “friend” of another class, or inherit from a parent class. So there are lots of ways for a class to access members from other classes without including a global state.

Edit: added the #include in ofApp.h in code above

Got it … it will take a bit to digest this but it is extremely helpful. Most of my OOP experience has been in Java with Processing, where the variable declaration/definitions seems “looser” and as I spend more time in the C++ world, I will need to be more disciplined. Once again this Forum is an amazing asset. Once I sort this out I will post what I end up doing.

1 Like

Also if you’re somewhat new to c++ but are familiar with Processing and java, you may want to look at using a std::vector instead of an array. ofBook has a chapter on that too: ofBook - Introduction to vectors.

And c++ has const types, where a value(s) assigned to a variable remains constant for its lifetime and can’t be changed. I can’t remember if java makes this distinction or not.

TimChi,
Thanks again for your insights and suggestions. The backstory on this effort is that I have dozens of somewhat complex apps developed using Processing and for a variety of reasons want to convert them to C++. Some have been easier than others. Those with multi-dimensional arrays have been problematic. I have used C++ vectors and variable pointers in a couple of the apps and find them useful.
It is just going to take time to determine how best to deal with the Java → C++ differences.
I very much appreciate your time and advice.

TimChi,

Your initial suggestion (above) worked as you expected in the simple example I posted above. I defined and initialized a variable in “aClass1” and was able to modify it in functions in both ofApp.cpp and aClass2.cpp using the variable name.

This gives me a much better sense of what I can do with a primitive variables and extern (so that is the answer to my initial question.) Now I need to decide if and how this may prove useful in my overriding project. Much Thanks.

Hey awesome! And it sounds like you have a pretty good start on c++. Porting projects from Processing to oF will be a fantastic way to learn more about the language. Have fun and glad you got the extern to work!