Index operator of a class that's being pointed to error (C++)

Started by
2 comments, last by Draders 16 years, 11 months ago
I overloaded the index operator [] in the test class below, but when I try to access it (int *fd = ds[0];) I get the error "cannot convert 'test' to 'int' in assignment" If I make ds not a pointer as in (test ds = test()) it compiles fine. Is what I'm trying to do impossible/stupid or am I missing something here? code: class test { public: test() { d = new int; *d = 5; } ~test() { delete d; } int* operator[](int index) { return d; } int* d; }; int main(void) { test *ds = new test(); int *fd = ds[0]; return 0; }
Advertisement
ds, as a pointer, needs to be dereferenced before use. This uglifies use of some operator overloads, you'll need to use:
int *fd = (*ds)[0]// or obtain a reference// can be useful if you are constantly using the variabletest &ds_ref = *ds;int *fd = ds_ref[0]

Or, you could create df on the stack [edit: which I've just noticed you've tried [smile]]
int main() {test ds;int *fd = ds[0];return 0;}
Quote:Original post by Draders
I overloaded the index operator [] in the test class below, but when I try to access it (int *fd = ds[0];) I get the error "cannot convert 'test' to 'int' in assignment"


'ds' is a pointer to a test, and using a subscript on any pointer treats it as, well, a pointer to an array. In your case, it means "grab the 0th element of the array presumed to exist and start at location ds". That would work in your case (because it points at a 'new Test', and "the 0th element" is the thing that is right there), but it would give you back that Test instance. A Test is not convertible to an int*, so the compiler complains about the assignment.

What you need to do is dereference the pointer first, so that you have a Test instance (and not a pointer thereto), so that you can invoke its operator[].



But you would still be doing something wrong, because there's no reason to create the Test instance dynamically anyway.

But you would still be doing something wrong, because operator[] overloads are supposed to pay attention to the provided subscript and act accordingly. It seems like the goal here is for the Test instance to hold an array of ints, and the operator[] overload to select one of those ints. So the natural return type is 'int', rather than 'int *', and we should in turn subscript into 'd' when we implement the operator.

But you would still be doing something wrong, because subscript operators normally come in pairs.

But you would still be doing something wrong, because the Test class allocates a single instance rather than an array. It doesn't make sense to subscript into something that doesn't logically "behave like an array".

But you would *still* be doing something wrong, because all the (single-dimensional) array-like functionality you could imagine in a class - at least, the functionality that doesn't require adding extra overhead - is already wrapped up in the standard library class std::vector.
The test class is a test and doesn't really represent what I'm actually returning. I'm returning 4 "background" objects. The default background object is abstract and thus I can't do "Background d"; only "Background *d". There are different kinds of backgrounds that inherit from the abstract one. For examle "Background *d = new RotationalBackground". The test class in reality also inherits from an abstract class and also needs to be a pointer. I just simplified it to figure out easily why it wasn't doing what I expected it to.

This topic is closed to new replies.

Advertisement