accessing functions

lets say i have an array of pointers to a list or a queue, and the list items that i insert in that list or queue are class object. So how would i go about accessing functions from the class object through the pointer in the array.

for example)

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

class example
{
private:
int number;

public:
example();
void setNumber();
int getNumber();
};

int main()
{
queue* exArray[];

for (int x = 0; x < size; x++)
{
exArray[x] = new queue;
}

// access setNumber() from array of queue, for which 
// example object is being inserted into.
// how do i do this ?

return 1
}

closed account (1yR4jE8b)
Well, when you access the array, the operator[] will return a pointer. You can treat that returned pointer like any other pointer. So to call a function using that pointer use the -> operator.

exArray[someIndex]->insert(someValue);
i know how to use the functions from the list or queue im adding to, but im asking how would i access the functions from the example class(ie. the object im adding to that list) from that array? thanks.

closed account (1yR4jE8b)
Ok, well it's basically the same idea, but if the queue returns a pointer you need to use the -> operator and if the queue returns a reference or value then you need to use the . (dot) operator.

1
2
3
4
5
//pointer
exArray[someIndex]->get()->someFunction();

//reference or value
exArray[someIndex]->get().someFunction();
Last edited on
Topic archived. No new replies allowed.