For simplicities sake, let's call the first class ClassA, and the class that needs to iterate over all the ClassAs the OverClass. To whenever you create a ClassA instance, OverClass needs to know about it.
How about making an OverClass object part of your ClassA constructor. Inside the ClassA constructor, you can call some .addClassA() method in OverClass, passing it the this pointer. Inside OverClass, you can keep track of all the ClassA instances through an array/vector/list/whatever of ClassA pointers.
Is it possible to use a static array of pointers inside the OverClass? I've tried this and I keep getting compiler errors that I cannot figure out how to resolve. I would appreciate it if you could troubleshoot my code.
[PHP]
//marker.h
#pragma once
#include "imganalyzer.h"
class Marker{
public:
Marker(int x, int y, double _avgHue, double _avgVal, double _hueStdDev, double _valStdDev);
//...more code...
};
[/PHP]
[PHP]
//marker.cpp
#include "marker.h"
#include "imganalyzer.h"
Marker::Marker(int x, int y, double _avgHue, double _avgVal, double _hueStdDev, double _valStdDev){
//...other code...
//register the existance of this instance with the ImgAnalyzer class
ImgAnalyzer::addMarker(this);
}[/PHP]
[PHP]
//imganalyzer.h
#pragma once
#include "marker.h"
class ImgAnalyzer{
public:
//the array that stores the pointers to the Marker objects
static Marker* allMarkers[25];
//Every instantiated marker invokes this method
//The pointer to that marker is added to the allMarkers array
static void addMarker(Marker* pMarker);
//This method iterates through all the Marker objects pointed to in the allMarkers array
static void relocateMarkers(IplImage* bgr);
};[/PHP]
[PHP]
//ImgAnalyzer.cpp
#include "imganalyzer.h"
using namespace std;
void ImgAnalyzer::addMarker(Marker* pMarker){
cout<<"adding Marker\n";
//I will be using a static indexing variable later
allMarkers[0] = pMarker;
}
//access an element from the pointer array
void ImgAnalyzer::relocateMarkers(IplImage* bgr){
Marker marker_i = *allMarkers[0];
//more code...
}
[/PHP]