It looks like you badly misuse header files. Also, you mess up unrelated concepts: separate compilation, access of instances and memory allocation.
It is very bad idea to put any objects in a header file. Put only types and method declarations (and some inline definitions, see below). Just thing of how include works and you will understand why.
This is a usual pattern:
MyClass.h:
class MyClass {
public:
MyClass(int value) : m_Value(value) { }
int Value() const { return m_Value; }
int SomeSeriousMethod();
private:
int m_Value;
};
MyClass.cpp:
#pragma once
#include <stdafx.h> //only if you use pre-compiled headers
#include "MyClass.h"
int MyClass::SomeSeriousMethod() { }
Usage:
#include "MyClass.h"
void SomeFunction() {
MyClass instanceOnStack(1);
int value1 = instanceOnStack.SomeSeriousMethod();
MyClass * instanceInHeap = new MyClass(2);
int value2 = instanceInHeap->SomeSeriousMethod();
MyClass * someOtherInstance = &instanceOnStack;
value2 = someOtherInstance->SomeSeriousMethod();
delete instanceInHeap;
}
—SA