Assigning multiple class objects to ints

Hey there.... I was wondering if you can assign multiple objects of a class (or any other date type for that matter) to an int?

For example:

I have to following objects :
1
2
3
4
5
6
7
8
9
10
11
12
    pTexLBB.render(pPosX, pPosY);
    pTexRBB.render(pPosX, pPosY);
    pTexLBT.render(pPosX, pPosY);
    pTexLCEB.render(pPosX, pPosY);
    pTexLCET.render(pPosX, pPosY);
    pTexLCOB.render(pPosX, pPosY);
    pTexLCOT.render(pPosX, pPosY);
    pTexRBT.render(pPosX, pPosY);
    pTexRCEB.render(pPosX, pPosY);
    pTexRCET.render(pPosX, pPosY);
    pTexRCOB.render(pPosX, pPosY);
    pTexRCOT.render(pPosX, pPosY);


Now all I'm doing is going through each and every object of my class and adding .render(int, int) function to it. I also do some other functions on them and my code gets quite huge... So can I somehow make it so that I can identify all my objects by ints, so that I can then use a for loop to set them all?

Thanks in advance for any help. :)
Last edited on
Hey, still pretty new to C++ but I think you might be able to make an array of pointers, and you can then assign each class to one pointer in the array. You can then just use a for loop with an index variable. Like this:

1
2
3
4
5
6
7
8
9
MainClassName* pPointers[5];
pPointers[0]=&Member1;
pPointers[1]=&Member2;
....

for (int nIndex=0; nIndex<5; nIndex++0
{
     pPointers[nIndex].render(pPosX, pPosY);
}


I'm not sure if this will work but I hope is helps!
Last edited on

You can create multiple objects like you do with an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>

// example class
class myClass
{
private:
		int someVar;
public:
	void SetVar(int var) { someVar = var; }
	int getVar() { return someVar; }

};

int main()
{

	// declare 10 objects (0-9)
	myClass Objects[10];

	// give them some values.
	for (int i = 0; i < 10; i++)
		Objects[i].SetVar(i + 1);

	for (int i = 0; i < 10; i++)
		std::cout << Objects[i].getVar() << std::endl;

	return 0;

}

1
2
3
4
5
6
7
8
9
10
Topic archived. No new replies allowed.