Sign in to follow this  

Refresher on &

This topic is 4340 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

In another thread, this came up:
class Vector3
{
private:
    float x, y, z;
public:
    float &X() { return x; }
    const float &X() const { return x; }

    float &Y() { return y; }
    const float &Y() const { return y; }

    float &Z() { return z; }
    const float &Z() const { return z; }

    float &operator[]( int i ) const
    {
        return ( float & )*( &x + i );
    }

    operator float *()
    {
        return &x;
    }
};
from what I remember if you did this:
int x = 3, y = 4, z = 5;
Vector vec3;

vec3.x() = x; //now vec3.x is 3 permanently
This next piece of code however eludes me. I'll just post and see if I can break it down. Feel free to correct.
float &operator[] (int i) const //passes in int. what does & do for this? 
{
    return (float &) //casts to variable to float reference
           *(&x + i); //takes the address of x and moves i places, then dereferences value
}
I'm still not seeing the purpose or how the & operator in that function. Dead end for me.

Share this post


Link to post
Share on other sites
The value is being dereferenced. Basically, you have a pointer that is moved "i" places in memory. That adress is taken, dereferenced, and then returned as a reference. It's taking the value of the pointer and returning a reference to it.

EDIT:

As for your question, what the reference does is allow you to directly access that variable. It's that vraible, and not a copy of it. So by doing that, you're modifying that variables value with a function.

Share this post


Link to post
Share on other sites
Seeing code like this is new for me, but I'm pretty confident it just returns a reference to the specified element. Because it's POD data, the { x y z } is listed sequentially in memory, so it's valid, if volatile.

Thus, when you dereference the memory address, you have a float, being either x, y, or z (or crazy, if you enter wrong numbers). It then passes this by reference, so you can change the value of it. It's a tenuous way of coding I wouldn't do, but it works.

Edit: Heh, "POD data". Redundancy at its best.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
I'm still not seeing the purpose or how the & operator in that function. Dead end for me.
It does the same thing as in the other examples, i.e. returns a non-constant reference to the member variable. The only difference is that the named member variables are coerced into 'looking' like an array, more or less.

Does that answer your question?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes

private:
float x, y, z;
public:
float &X() { return x; }
const float &X() const { return x; }

float &Y() { return y; }
const float &Y() const { return y; }

float &Z() { return z; }
const float &Z() const { return z; }



BTW, this is not good form. Nothing at all is gained by these functions; they offer exactly *zero* protection and do nothing to separate interface from implementation. You should *seriously* consider just making the component scalars public and be done with that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Quote:
Original post by Alpha_ProgDes

private:
float x, y, z;
public:
float &X() { return x; }
const float &X() const { return x; }

float &Y() { return y; }
const float &Y() const { return y; }

float &Z() { return z; }
const float &Z() const { return z; }



BTW, this is not good form. Nothing at all is gained by these functions; they offer exactly *zero* protection and do nothing to separate interface from implementation. You should *seriously* consider just making the component scalars public and be done with that.

I didn't create that code. As I said, it was from another thread. The title is "... in C++". Could someone show how that

float &operator[]( int i ) const
{
return ( float & )*( &x + i );
}

code is supposed to work in actual code?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
I didn't create that code. As I said, it was from another thread. The title is "... in C++". Could someone show how that

float &operator[]( int i ) const
{
return ( float & )*( &x + i );
}

code is supposed to work in actual code?


Ok, basically x, y, and z are declared in that order right next to each other, so they should be adjacent in memory.
So if you obtain a pointer to x (&x) and then you add an integer to that pointer (i.e. &x + 1) you will end up with the address of the data at sizeof(x) bytes times the integer past x.
So (&x + 0) is the address of x, (&x + 1) is the address of y, etc. (note however that (&x + 3) would point somewhere bad, so it would be a good idea to assert(i >= 0 && i < 3) )
Then we de-reference the pointer (i.e. *(&x + 1) ) to get the value pointed to, and we return a reference to it.

Share this post


Link to post
Share on other sites

This topic is 4340 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this