Skip to main content
gamedev.net
🔒 Locked

Operator ambiguity

Started by CraZeE 4 replies 1.2k views
Original Post
CraZeE
CraZeE
hi guys, this concerns an issue with operator overloading. in my vector class, i tried setting overloaded operators for the array index and for casting. however, i cant seem to have both at the same time. sample:
  
const float operator [] ( const long &index ) const;
operator float*() const;
 
now the error i get says i have 2 ambiguous definitions for the [] operator. it seems to make sense since the array index operator is usually interpreted as a pointer too. but the difference between the operators lies in that one takes a parameter and the other doesn't. yet the code won't compile. if i leave only one and not the other (doesnt matter which one) then it works. i use MSVC6. so it could be a compiler problem. i checked the msdn for the reported error and the example case provided does not coincide with mine entirely. help is appreciated. thanx. [edited by - crazee on April 21, 2003 1:07:50 PM]
To learn, we share... Give some to take some -
Sneftel
Sneftel
Yeah, the problem is that the compiler has the choice of using your subscript operator, or implicitly converting to float* and then using the built-in subscript operator. This is why typecast operators are generally a really, really bad idea.

What are you trying to do here? Why do you want to convert to a float*?


How appropriate. You fight like a cow.
sjelkjd
sjelkjd
Don''t provide an implicit operator to float. If you want an easy way to call glVertex3fv with your vector class, you should make an overloaded version:

  
void glVertex3fv(MyVector3& v)
{
glVertex3fv(&v[0]);
}

You can do the same for whatever else you needed to cast to float* for.
cgoat
cgoat
Maybe your overloaded operator[] should return a reference instead?
CraZeE
CraZeE
sneftel: the reason i''m using explicit cast operator is coz i''m using it in a vector math class. the vector parameters are defined as a union of 4 float parameters, public too. now it is already convenient to access the values as x,y,z OR v[0...2].

i just wanted to overload the index operator[] in case i REALLY get lazee but the cast operator comes in handy in case i want to directly cast the vector into an array of floats:

without my intended approach:

float *list = vectorTemp.v; OR
float *list = &vectorTemp.x

with my intended approach:

float *list = (float*)vectorTemp;

it felt simpler for me to work with the casting way, though i think it is slower. anyway, it was also because i wanted to learn a bit about operator overloading. guess i got carried away. i scrapped the casting operator though.. found the index operator more useful.
To learn, we share... Give some to take some -
CraZeE
CraZeE
cgoat: ur suggestion doesn''t work coz the issue doesnt seem to be with return type. in fact wat boggled me initially is y does ambiguity even exist when the parameter list for the operators differ.

using the operator[] REQUIRES an index integral, but the cast doesnt need. so y does msvc go through all the trouble of trying to do implicit casting before indexing instead of simply using my self-made operator. kinda irritating when the compiler stops listening to you.. grrr
To learn, we share... Give some to take some -

Topic Locked

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

Sign in to reply to this topic.