Jump to content
  • Advertisement
Sign in to follow this  
Draders

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

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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 variable
test &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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!