Pointer to a 2d array of classes!

Started by
4 comments, last by CoiN 23 years, 8 months ago
Lo All, What I wondering was, whether it is possible to use array indexing with the pointer to the class, when I tried to do it like this: p[outer][inner]->setnum(outer+inner); I got errors.... I also tried using pointer arithmetic like this: *(p + (inner*outer) + 1)->setnum(outer+inner); // Prolly totally wrong Got an error... So I''m just asking if there are better ways use pointers to 2d arrays of classes than the way I''ve done, i.e. array indexing, or if I''m doing the above things totally wrong. This is the program.....
    
#include <iostream>
using namespace std;

class test {
	int num;
public:
	void setnum(int n) { num = n; }
	int shownum() { return num; }
};

int main()

{
	test tester[5][5], *p;
	int inner, outer;

	p = (test *) tester;

	for (outer=0; outer<5; outer++)
		for (inner=0; inner<5; inner++)
		{
			p->setnum(outer+inner);
			p++;
		}

	p = (test *) tester;

	for (outer=0; outer<5; outer++)
		for (inner=0; inner<5; inner++)
		{
			cout << p->shownum() << "\n";
			p++;
		}

	return 0;
}
    
Thanks in Advance.... P.S. Try not to call me stupid too many times
Advertisement
I believe you need to use a double pointer when pointing to 2d arrays, (triple pointer to 3D arrays, and so on) So when accessing a part in your array of classes it would go like this:

class name
{
public:
int hi;
};

void main(void)
{
name array[num1][num2], **p;

p = array;

p[sec1][sec2].hi = value;
}

hope that helps
I modified the code to use a pointer to a pointer and i still get errors.....The new code is below anyone got any ideas?

    #include <iostream>using namespace std;class test {	int num;public:	void setnum(int n) { num = n; }	int shownum() { return num; }};int main(){	test tester[5][5], **p;	int inner, outer;	p = (test **) tester;	for (outer=0; outer<5; outer++)		for (inner=0; inner<5; inner++)		{			p[outer][inner]->setnum(outer+inner);		}	for (outer=0; outer<5; outer++)		for (inner=0; inner<5; inner++)		{			cout << p[outer][inner]->shownum() << "\n";		}	return 0;}    


Thanks in advance....
Yea the AP was right

you have to use the . operator instead of the -> operator

p[outer][inner].setnum(outer+inner); //like this

The [] operators already dereference the double pointer twice, and the -> operator would dereference the pointer it again giving you an error.

But if you wanted to do it using pointer arithmetic you can do this

(*( *(p + outer) + inner )).setnum(outer+inner);

or this

( *(p + outer) + inner )->setnum(outer+inner);

Hope that helps

Edited by - Doctor Gonzo on August 27, 2000 5:52:32 PM
A man on the move, and just sick enough to be totally confident.Damn Right!
You could use the derefrencing operator.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
Thanks a lot guyz

This topic is closed to new replies.

Advertisement