Advertisement

hrmm, constant references not working?

Started by December 06, 2000 09:18 AM
3 comments, last by Succinct 24 years, 1 month ago
alright, guys, i''m defining a & operator for my Vector3d class... it''ll return a pointer to float of my 3 values (x,y,z) like this:
  
class Vector3d
{
    float x,y,z;

    float* operator &( void ) { return &x }
}[/source]

the problem is i''m not sure that x y and z are guaranteed to be allocated next to each other, so y might not be &Vector + 1..

what i decided to do was to make a static array of 3 floats, and assign x,y, and z to each of the elements as a reference
like:
[source]
class Vector3d
{
   float LinearData[3];
   float& x,y,z;

   Vector3d() : x(LinearData[0]),y(LinearData[1]),z(LinearData[2]);
}  
that way, it''s guaranteed... however.. x always = 0 now... any ideas? should''nt this work? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I have no name that you may call me. I am merely Succinct. ~Succinct Demos Online~ "Hey, where''d that display list rotate off to now?" -(Drop me a line here)-
-- Succinct(Don't listen to me)
Maybe union is right approach to your problem?

DLife
Advertisement
quote: Original post by Succinct
class Vector3d{   float LinearData[3];   float& x,y,z;   Vector3d() : x(LinearData[0]),y(LinearData[1]),z(LinearData[2]);}   

however.. x always = 0 now...


Well, here''s a big problem: float& x,y,z is equivalent to float &x, y, z;

y and z are floats, x is a reference to a float.

I do this all the time myself, i.e. always use Type* obj instead of Type *obj, but you should be aware that the correct way to state what you want is:

float &x, &y, &z

If x is always zero, look at the array LinearData and make sure it has the correct values.
I''m not completely sure as to why you are overloading the & operator.

But why do you not simply return the this pointer.

Add some access operators or make your member variables public.




i always do this stuff..

i typed the code off the top of my head...

u guys r all correct...

i do normally declare it as float &x,&y,&z...
sorry...

and x,y,z are public...

the reason i do is to use things like glVertex3fv( &Vector );
i guess i''m just being silly and trying to save space overloading the & operator to avoid things like Vector.GetPtr() or even Vector()...

why wouldn''t LinearData[0] have the correct value? hrmm

aren''t these two examples the same,
if given:
float &x = LinearValue[0];
then
x = 5;
LinearValue[0] = 5;

?

hrmm..

thx so far, guys


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have no name that you may call me. I am merely Succinct.
~Succinct Demos Online~
"Hey, where''d that display list rotate off to now?"
-(Drop me a line here)-

-- Succinct(Don't listen to me)

This topic is closed to new replies.

Advertisement