This sounds like VB except for the references to .h and .cpp. In this instance you may want to make class A a friend of class B. I don't normally recommend this and normally using friend indicates to you that your class structure is seriously flawed, but there are certain times when a friend is the only way to get the job done. Usually those instances are when you want class A to manage all instances of class B, but you don't want the user to be able to explicitly instantiate class B. They must go through class A to instantiate class B. Of course then class A would need to be a singleton class to avoid more issues.
But since A and B do not have any prior knowledge of each other and are not derived from each other this is sometimes the only way.
Code:
class B;
class A
{
static friend class B;
static vector<B> BList;
public:
A () {};
static unsigned int CreateB(void)
{
B *temp=new B;
BList.push_back(temp);
..set unique ID for new B
return ID;
}
static B GetB(unsigned int _ID) {return BList[_ID];};
private:
//Hide copy constructor to prevent misuse and/or improper implicit instantiation
//Just in case somehow someone does get access - always assert as false
//Fail in all cases
A(A& obj) {ASSERT(false);};
};
class B
{
private:
B() {}
};
I coded this while sitting here so I'm fairly sure it doesn't work as shown. But it shows the general principle where a friend is pretty much the only way to get the job done.