Is declaring a static pointer to in an object in one .cc file, then returning that pointer with a function to a second static pointer in another .cc file safe or bad practice? I have two static pointers in file_a.cc and file_b.cc but use a function to make the pointer in file_b.cc point to the same object declared in file_a.cc. It feels like I'm doing something very bad. Am I? If I call foo() and then print_object() it will print 1, so the pointers are pointing to the same object.
/** file_a.h */
#ifndef FILE_A_H
#define FILE_A_H
struct Object {
int value = 0;
}
Object* get_object();
void print_object();
#endif
/** file_a.cc */
#include "file_a.h"
static Object* object = new Object();
Object* get_object() {
return object;
}
void print_object() {
std::cout << object->value << std::endl;
}
/** file_b.h */
#ifndef FILE_B_H
#define FILE_B_H
#include "file_a.h"
void foo();
#endif
/** file_b.cc */
#include "file_b.h"
static Object* object = get_object();
void foo() {
object->value += 1;
}