H
hlg
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
So far I have tried implementing this as:
//
class myObj {...};
//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;
....
myObjRow myObjArray;
//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}
So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:
#define myObjArray[x,y] GetMyObj(x,y)
.... as I get the error message
"error C2008: '[' : unexpected in macro definition"
So, has anyone a solution ? (Or am I on the wrong track entirely ?)
hlg
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
So far I have tried implementing this as:
//
class myObj {...};
//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;
....
myObjRow myObjArray;
//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}
So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:
#define myObjArray[x,y] GetMyObj(x,y)
.... as I get the error message
"error C2008: '[' : unexpected in macro definition"
So, has anyone a solution ? (Or am I on the wrong track entirely ?)
hlg