lundi 18 octobre 2021

Make extern variable can't be accessed in specific files

So I have:

foo.h

#ifndef FOO_H
#define FOO_H

extern int bar;

void change_bar_value(const int& value);

#endif

foo.cpp

#include "foo.h"

int bar = 10;

void change_bar_value(const int& value)
{
    bar = value;
}

and main.cpp

#include "foo.h"

int main()
{
    bar = 20;
    change_bar_value(20);
}

So I want that you can't direcly change bar in main.cpp, but you can call a function that changes the value of bar. So how can I do it?

Aucun commentaire:

Enregistrer un commentaire