سؤال

I am trying to access an array of class instances from two source files, was hoping you could point me in the right direction. Here's roughly what I have so far.

//X.h    
extern Object myObj[5];

//A.cpp
#include X.h

Object myObj[5];

myObj[0].doSomething();
...
myObj[4].doSomething();


//B.cpp   
#include X.h

Object myObj[5];

myObj[0].doSomethingElse();
...
myObj[4].doSomethingElse();

I have entirely no idea if I am along the right lines. Even a phrase or two for me to google would be much appreciated.

هل كانت مفيدة؟

المحلول

You could get a pointer to the array. so int file "A"

Object arr[5];

Object* GetArr(){
   return &arr;
}

and file "B"

#include "a.cpp"
void DoSomething(){
    Object* pArr = GetArr();
    //use pArr
}

That may work for your situation. Good Luck

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top