1

I have three files, a main .cpp file:

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



int main()
{

    myClass mvar;

    tryVar = 23; // why does this not work?

    printf("%d ", mvar.readTryVar()); // This writes out 0, why??

    return 0;
}

a myClass.cpp file

#include "myClass.h"


myClass::myClass(void)
{
}


myClass::~myClass(void)
{
}

void myClass::setTryVar()
{
    tryVar = 23334;
}

int myClass::readTryVar()
{
    return tryVar;
}

and a myClass.h file

#pragma once

static int tryVar;

class myClass
{
public:
    myClass(void);
    ~myClass(void);


    void setTryVar();

    int readTryVar();
};

They're very simple files, however I can't understand why the static variabile isn't set in the main function and I need to set it through the myClass functions.

I think that I don't know very well how the "translation units" are created, I know that the "include" directive simply copies the content of the header file into the .cpp file before the actual compilation.. then why isn't the static variable visible?

paulAl
  • 949
  • 2
  • 10
  • 17

3 Answers3

8

static has multiple meanings. Outside a class, it declares a variable that is unique to every translation unit, so main.cpp and myClass.cpp have their own copies.

To accomplish what you want, you need an extern variable:

//myClass.h
extern int tryVar;

//myClass.cpp
int tryVar = 0;  //definition needed for extern variable
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

Broadly, you can think of each .cpp file as a translation unit. Everything else gets included into it with #include. Thus, since both your .cpp files include myClass.h, they both define a static variable named tryVar. You have two variables with the same name, and each code file reads and writes its own copy. They cannot see each other's copies.

If a variable is to be accessible from multiple translation units (.cpp files), then it should not be static. Rather, it should be declared with extern in the header, and then defined in one translation unit. See the past Stack Overflow question What are extern variables in C?

Change your header to declare the variable:

extern int tryVar;

Change myClass.cpp to define it:

int tryVar;

With those two changes, you can read and write the same variable throughout your program.

Generally speaking, if you're using static on a global (i.e., non-member) function or variable in a header, you're probably doing it wrong. (Using static on member functions and variables in a header is fine, though.) Only use global static in .cpp files.

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
1
  1. When a static variable is declared outside of the function & classes the variable has file scope, in this case the tryVar is unique to file where the myclass.h is included,so with our problem we have two unique version of tryvAr one for myclass.cpp & other for main.cpp

  2. When a static variable is declared & not initialized the compiler automatically initializes the variable to zero.

Now coming to your problem

  1. The declaration of the static variable tryVar is in myclass.h & since the declaration of the tryVar is outside of all functions & classes , the variable is file scopic i.e., The variable is visible to the files wherever the myclass.h is included but each file have their own unique copy.

  2. tryVar = 23; // why does this not work?

    This actually works but not the way you intended , because what you are trying to print is the tryVar variable which is available(unique) to the myclass.cpp via the object of class mycalss,

    Print the tryVar directly in the main file & you can see the output as 23 .

  3. printf("%d ", mvar.readTryVar()); // This writes out 0, why??

    This returns zero because you are trying to the print the tryVar unique to the myClass.cpp & since you have not called the setfunction the compiler initiaLIzes the variable to zero ,thats why you get zero,

    Try calling the myClass::setTryVar() function in the main file before printing the tryVAr unique to the myClass.cpp file.

Solution

solution 1 :

make the tryVar extern i.e., the tryVar is global, only one copy of tryVar exists

Cons: The problem with this solution is you don't have control over the variable

Solution 2:

bring the tryVar as static data member of the class & define it in myClass.cpp , by doing this way you can control the visibility of the variable

Ram
  • 1,115
  • 8
  • 20