I want to be able to instantiate and use the same object instance in several .h files so that: a) that object can be instantiated once and then re-used in several places b) the value for one of the variables of that object instance is to be updated later in the code and thus available to all of the functions which are set to use it
Example:
Let's say the object is called: AdressHelper and it is defined in AdressHelper.h. Like this:
class AddressHelper {
private:
uint32_t pid;
DWORD_PTR baseAddressPtr;
public:
AddressHelper(uint32_t pid) {
this->pid = pid;
this->baseAddressPtr = this->GetProcessBaseAddress(pid);
}
DWORD_PTR GetProcessBaseAddress(DWORD processID)
{
DWORD_PTR base_address = 0;
//... initialize base_address with something
return base_address;
}
DWORD_PTR GetBaseAddress() {
if (this != NULL) {
return this->baseAddressPtr;
}
}
void SetBaseAddress() {
if (this != NULL) {
this->baseAddressPtr = GetProcessBaseAddress(pid);
}
}
};
Now, I have Globals.h which describes this object like this:
#pragma once
#include "AdressHelper.h"
#include <cstddef>
extern AddressHelper* addressHelper;
If I instantiate addressHelper in file gui.h
#pragma once
#include "world.h"
#include <array>
#include "Globals.h"
AddressHelper* addressHelper = new AddressHelper(GetCurrentProcessId());
I can then use addressHelper in gui.cpp
addressHelper->GetBaseAddress() + 0xb81074;
But how do I now refer to the same instance of addressHelper in world.h for example?
I can't do #include "Globals.h"
for example in world.h as compiler starts to complain that
addressHelper is already instantiated in gui.obj.
The reason is probably as world.h is already included to gui.h and there is cycle dependency. But I can't drop that link for now and still want to do what I have stated above. Any way to achieve this?