473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unable to iterate over all instances of a class

Hi all,
I am developing a class, and I need the ability to iterate through every single instance of that class in another class. I have not been able to write any code that will compile. How can I structure my code so that I am able to iterate through all the instances?
Oct 21 '07 #1
5 2599
Ganon11
3,652 Expert 2GB
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.
Oct 21 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
[anonymous]
am developing a class, and I need the ability to iterate through every single instance of that class in another class. I have not been able to write any code that will compile. How can I structure my code so that I am able to iterate through all the instances?
[/quote]

This is a classic requirement for the Observer design pattern. Research this pattern.
Oct 21 '07 #3
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]
Oct 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
If you don't want to use an Observer, then you might keep the addresses of your objects in a vector.
Oct 22 '07 #5
Thanks for all of your help. I was able to correct and compile my code. The reason for my compiler errors was that I had a circular dependency. The static array of pointers to my class works now.
Oct 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: MKoleoso | last post by:
Problem: C#- Unable to create instance of a class implementing from an interface I have: namespace someNamespace { public __gc class SomeClass1 { }
4
by: wardellcastles | last post by:
I am running SQLServer 2000 SP3 with MDAC 2.8. On instances created via MSDE, I am unable to create the first step in a job. Instances created with SQLServer 2000, this problem does not occur....
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
11
by: ItsMe | last post by:
Hi, I've 50 MDI Forms in my project, so trying to create MDI Child Form from this procedure. But the problem is, unable to declare as "NewFormName". It gives me an error. Is there any other...
16
by: Crirus | last post by:
I have a class. I need to write a routine in this class, that loop through it's members (in a instance of the class) and concatenate all members values as string. I need to filter does members...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
4
by: =?Utf-8?B?TWF0dCBMb3Zl?= | last post by:
If I extend the WebBrowser class like this: public class CustomWebBrowser : System.Windows.Forms.WebBrowser { public string myProperty() { return "myProperty"; } }
1
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, Is there a way ( perhaps throgh refleksion) to iterate through the colors of the System.Drawing.Color struct. foreach (Color color in .... i dont know.... regards Jesper
2
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.