c++ - Passing array of objects among classes -


afternoon all, first question on stack overflow -- best clear.

i have created array of objects "objects2[arraysize]" class "class 2" must referenced both in class 1 , main() function.

class1.h

class class1 { public:   void parsefunction(several parameters);  private:   int othervariables;  }; 

class1.cpp

#include "class1.h"  void class1::parsefunction(several parameters) {   for(int = 0; < arraysize; i++)   {     objects2[i].somememberfunction();   } } 

main

#include "class1.h"  main() {   class2 objects2[arraysize];    //arbitrary array size   class1 object1;    object1.parsefunction(some parameters);    for(int j = 0; j < arraysize; j++)   {     objects2[j].someothermemberfunction();   } } 

i have simplified best of ability, , point of readability. goal able access these objects "objects2[arraysize]" multiple classes, struggling getting pointers work. have intentionally opted use fixed array size rather vector attempt simplify code time being.

as stands right 1 class (err, main or class 1) can access these objects , functions @ time. looked making class 1 act 'pass through' functions in class 2, feels hokey. there more elegant way? pointers?

edit: clear, not have firm grasp of how objects may shared among different classes. goal create large number of these "objects2[arraysize]" objects (accomplished creating array of them), , access them in several different classes. have researched passing reference, can't quite right array of objects.

this may specific answer while looking more generic one, include array member of class1 , provide public access this.

class class1 {   public:     class2 objects2[arraysize];     void parsefunction(several parameters);    private:     int othervariables;  }; 

in main program, can access object1.objects2. if don't want tied instance, make static member.

however, better way may hide actual member , provide accessors (get/set). depends on trying do.

if wanted pass reference, change method in class1 follows:

void class1::parsefunction(several parameters, class2& objects2[]) 

and pass in parameter main , reference in parsefunction method.

another method include "globals.h" file defines things want accessible everywhere.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -