Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

operator[] function parameter declaration

Started by jakesee Sep 16, 2010 at 5:46 AM 1 replies 550+ views
Original Post
jakesee
jakesee
Hi,

Wonder if anyone knows the syntax for this construct if it is possibe:

I want to write a function that accepts an object(class) that has defined the index operator: Vector3 operator[](int unsigned);

In case i am not clear, let me give an example. suppose I have a function as such:

// accepts an array, with count elemtents
// dowork() will index array using array[count];
void dowork(Vector3* array, int count);

In this above, the type Vector3* has operator[]() defined.

Incidentally, I am using boost::thread and it allows any object with operator()() defined to be supplied as the thread object. So I think this contruct is highly possible. But I do not know how.

Anybody knows how to do this?
Thanks in advance!

-------------------------------
Longer story:

I wrote a template class to access interleaved arrays sequentially using stride and starting address through the operator[].

Now I want to write a function to be able to acess this template class, but be agnostic about whether the supplied object it is a plain-old-pointer or actually a specialized "pointer".
rip-off
rip-off
Use templates:
#include <vector>struct Vector3 {};struct Example {    Vector3 &operator[](unsigned int) {        static Vector3 instance;        return instance;    }};template<typename VectorArray>void dowork(VectorArray &array, int count){    Vector3 &vec = array[0];    // use vec}int main(){    Vector3 array[3];    std::vector<Vector3> vector(3);    Example example;    dowork(array, 3);    dowork(vector, 3);    dowork(example, 3);}

That should work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.