Archived

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

N0T0R10US

C++ Passing Multidimensional array pointer to Class Member Functions

Recommended Posts

What''s the best approach for passing a pointer to a char multidimensional array to a class member function ? I''m confused with the precedence/syntax. My function prototype - Class::Class(char *c) If I initiate with - Class MyClass[5][3]={"Im ","Too ","Lazy"); I''m creating three instances of my object. That''s not what I want. I can see a way of doing this over more than one line using MyClass[0][0] as the ''base'' pointer. Is there an eliquent way ?

Share this post


Link to post
Share on other sites
ps This is related to my last post. I''m trying to build a particle engine where the patterns are defined/mapped in strings. It''s the effect in some shoot''em ups where you hit something and it explodes into the score. I already have a basic particle engine but want to get this mapping licked !!!

Share this post


Link to post
Share on other sites
If you want your class to accept a multidimensional array you should change the class constructor to this:

Class::Class(char (*c)[])
//pointer to a 2-d array

quote:

Class MyClass[5][3]={"Im ","Too ","Lazy");



In this line you''re actually telling it to create a 5 by 3 array holding Class for each of it''s elements. Thus you''re actually creating 15 instances of your class from this statement, three of which are initialized.

But from what you explained it sounds more like you want to create one instance of your class and passing a pointer to multi-d array to it.





You fight like a dairy farmer.

Share this post


Link to post
Share on other sites
Cheers Greatwolf...

With your constructor example... how do I create an instance, passing the 2d array as a pointer? Can I do this on one line?

I''m getting tied up in the syntax of creating multiple instances when all I want is one.

Share this post


Link to post
Share on other sites
I suppose you could do something like this from the calling function that's making the object for this class:


char cArry[5][4]={"Im","Too","Lazy"};
Class MyClass(cArry);
//or if you want everything on one line maybe this might work
Class MyClass({"Im","Too","Lazy"});
//actually this kinda looks wierd to me


I think that's how it should look like but didn't test it. There might be syntax errors hehehe





You fight like a dairy farmer.

[edited by - Greatwolf on July 4, 2003 4:58:26 PM]

Share this post


Link to post
Share on other sites
Ta..

I''ve got the multi-line version working... but the single line is still bugging me....

Maybe I''m hung up the multidimension aspect when a 1d / stream will work if the class understands the format of the data.

I''d hate to think I''m missing something !

Share this post


Link to post
Share on other sites