Skip to main content
gamedev.net
🔒 Locked

& vs. * data members.

Started by GekkoCube 9 replies 1.3k views
Original Post
GekkoCube
GekkoCube
what is the difference (if any) between the two following setups: setup 1:

class Box
{
public:
   void set(Data *pData)
   { 
     m_pData = pData;
   }

private:
   Data * m_pData;
};

setup 2:

class Box
{
public:
   void set(Data &data)
   { 
     m_data = data;
   }

private:
   Data &m_data;
};

Am I confusing myself here or do the two accomplish the same thing?
Sure Shot
Sure Shot
They don't. Data &data is a reference, which is like a dereferenced constant pointer. So, Data &data would really be the equivalent of:
const Data **data;
References are mainly used in the copy-constructor and to pass objects to functions.
izzo
izzo
The second example won't actually compile. You can't assign references willy nilly. Once you assign a reference type, it always points to the same object; you can't re-assign it. You could do this:

class Box{public:   Box(Data &data) : m_data(data) { }private:   Data &m_data;}


But you couldn't re-assign what m_data points to. So if you want to be able to re-assign the data that Box stores, then you shouldn't be using a reference.

Data &data isn't the same thing as const Data **data since the second means a double pointer, not a dereferenced pointer!

cheers
sam.
GekkoCube
GekkoCube
thanks izzo for the insight.

but it seems like i can re-assign values to the reference of data.

i tried this on a primtitive data type (int).
havent tried it with an object yet.

izzo
izzo
Hm I was under the impression that you couldn't do that, and that's what the C++ faq (question [8.5] How can you reseat a reference to make it refer to a different object?) and msdn say, but I just did a test using gcc and it worked.. You will have to initialise the reference in your constructor though, and it will have to be done using initialiser lists (the reference has to refer to something).

sam.
Xai
Xai
Gekko, find out what the second example DOES ...

does it make the reference refer to a different object, or does it assign the value of the reference on the right, into the value of the reference on the left ...

in other words:

myRef = parameterByRef

could do a pointer assignment (if it is like example 1)

or could do an operator = on the underlying object (if it is like the C++ FAQ says it is) ...

also keep in mind, that 1 or 2 compilers doing something is very important, but you should also find out what the standard says should happen, so you know how much faith to put in the behavior being the same in other compilers (and even future versions of the same compiler).
GekkoCube
GekkoCube
hmm. good point there.

ill stick with what's been documented.
im not even up-to-speed with what the C++ standard is.

thanks.
Samurai Jack
Samurai Jack
Quote:
Original post by Sure Shot
They don't. Data &data is a reference, which is like a dereferenced constant pointer. So, Data &data would really be the equivalent of:
const Data **data;
References are mainly used in the copy-constructor and to pass objects to functions.


Are you sure &source means
const Data **data;
with 2 stars (*)? I belive it is just
const Data* data;
Feel free to correct if I'm wrong.
Sure Shot
Sure Shot
Yes, because you have to bear in mind that a reference is like a (constant) dereferenced pointer, so you would need the dereferencing operator as well.
MaulingMonkey
MaulingMonkey
Quote:
Original post by GekkoCube
what is the difference (if any) between the two following setups:


The first one (using pointers) will set the pointer to the new set of data.
The second one (using pointers) will call Data::operator=( m_data , data );

To explain in a single function:

void function ( void ){    int a = 1;    int b = 2;    int * pointer;    pointer = &a; //pointer now points to a. (a == 1)&&(b == 2)&&(*pointer == 1)    pointer = &b; //pointer now points to b. (a == 1)&&(b == 2)&&(*pointer == 2)    *pointer = 3; //pointer now points to b. (a == 1)&&(b == 3)&&(*pointer == 3)    int & reference ( a ); //must allways initialize a reference!!!    //could be written as int & reference = a; , but I prefer not to.    reference = a; //same as a = a. (a == 1)&&(b == 2)&&(reference == 1)    reference = b; //same as a = b. (a == 3)&&(b == 3)&&(reference == 3)    reference = 4; //same as a = 4. (a == 4)&&(b == 4)&&(reference == 4)}


Quote:
Am I confusing myself here or do the two accomplish the same thing?


Yep. The second version using pointers would look like:

class Box{public:    void set( Data * pData )    {        *m_pData = *pData;    }private:    Data * m_pData;};
MaulingMonkey
MaulingMonkey
Quote:
Original post by Sure Shot
They don't. Data &data is a reference, which is like a dereferenced constant pointer. So, Data &data would really be the equivalent of:
const Data **data;
References are mainly used in the copy-constructor and to pass objects to functions.


You mean:

Data * const data;

Notes: one asterix, Data is mutable (so no const before the *) but the pointer itself is not (hence the const after the *).

a constant reference (that is, const Data & data) would be akin to:

const Data * const data;

Topic Locked

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

Sign in to reply to this topic.