this is easier
You don''t really need to do anything. Take this class for example (I''ll use a static array, but it will work the same with dynamic arrays as long as you allocate them properly, and not in the array-of-pointers-to-array method):
class Foo
{
private:
int data[100][100];
public:
int* operator[](int i)
{
return(data);
}
};
That''s all you need.
When you try to do the following:
Foo bar;
int x = bar[10][20];
the operator[] function returns the address of bar[10] and then dereferences that address + 20. Like ((int*)bar[10])[20].
You can think of it like this:
Foo bar;
int* ptr;
int x;
ptr = bar[10];
x = ptr[20];
But it does it all in one quick step.
What's the meaning of this post ?
Why, if one ever uses static arrays, create a class for it ?
And if creating a class, why not make it dynamic for reusability ?
And, btw, The [] was cut out of your code because I is for italic fontset
-Markus-
Edited by - Cygon on September 29, 2000 4:17:24 PM
Why, if one ever uses static arrays, create a class for it ?
And if creating a class, why not make it dynamic for reusability ?
And, btw, The [] was cut out of your code because I is for italic fontset
class Foo { private: int data[100][100]; // public: inline int *operator[](int num) { return(data[num]); } };
-Markus-
Edited by - Cygon on September 29, 2000 4:17:24 PM
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement