Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CoiN

Pointer to a 2d array of classes!

This topic is 6538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks a lot guyz

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!