0

I'm trying to figure out a good way to share data across multiple files, but I'm sure some methods are better than others, so I'm coming here to ask what are the safest ways to share data in this manner. In the following example, I'll show how I've been doing it up until now, but I feel like this is not really the best way to do so.

Let's take an example where I have 5 files: file1.h, file1.cpp, file2.h file2.cpp, and main.cpp. They might look something like this:

//main.cpp
#include "file1.h"
#include "file2.h"

int main(){
    PushOne pushOne;
    PushTwo pushTwo;

    pushOne.Push();
    pushTwo.Push();

    for (int i =0; i<q.size(); i++){
        std::cout << q.front() << std::endl;
        q.pop();
    }
    return 0;
}

//file1.h
namespace Foo{

    extern std::queue<int> q; //resource to be shared across files

    class PushOne{
    public:
        void Push();
    };

}

//file1.cpp
#include "file1.h"
namespace Foo{

    std::queue<int> q;

    void PushOne::Push(){
        q.push(1);
    }
}

//file2.h
#include "file1.h" //#include this to have access to namespace variables declared in this file...Seems sort of inefficient
namespace Foo{

    class PushTwo{
    public:
        void Push();
    };
}

//file2.cpp
#include "file2.h"

namespace Foo{
    void PushTwo::Push(){
        q.push(2);
    }
}

So here, I have a namespace variable std::queue q that I want to access in both file1 and file2. It doesn't seem to make sense that I should have to put this namespace variable into one of the two files and just #include the other file. Is there some better method of doing this? This seems to grant sort of "unsymmetrical" access to the std::queue<int> q. I don't even know if this is necessarily a negative thing, but maybe somebody could shed some light on the efficiency of this method or propose another method.

Scorch
  • 437
  • 1
  • 3
  • 14
  • 2
    Global mutable data looks like not very good idea. Take a look at some explanations on SE: http://stackoverflow.com/questions/484635/are-global-variables-bad, http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil. Namespacing does not make global data less global. – Sergey Jul 12 '16 at 15:24
  • 1
    If you really want global data, consider Singleton (anti)pattern: https://en.wikipedia.org/wiki/Singleton_pattern – Sergey Jul 12 '16 at 15:26
  • 1
    You can and should use a [lock](http://www.cplusplus.com/reference/mutex/mutex/lock/) to ensure your queue is thread-safe after following all of these suggestions. – Akshat Mahajan Jul 12 '16 at 15:27

1 Answers1

2

If you really want to do this, define a struct with a static element in a header file:

MyQueue.h:

struct MyQueue {
   static std::queue<int> q;
}

You will also have to define the variable in a corresponding .cpp file.

MyQueue.cpp:

#inclue "MyQueue.h"

static std::queue<int> MyQueue::q;

You can access it by any file including that header, for example:

MyQueue::q.push(2);

Still this is something that I would not recommend, especially if there are multiple threads, because it is a global variable.

Another option would be to use a singleton, but this has the same issues.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45