C++, the & operator

Discussion in 'Programming/Html' started by The Chubu, Jul 3, 2012.

  1. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Hi, i've seen that in some of the examples of an OpenGL tutorial i'm reading, the parameters for some functions are entered with the & operator.

    Now, i know that if you use the & operator in a function like this:

    void function (int& param) {blah blah}

    The parameter enters as a reference instead of a value by putting the & operator as a suffix in the type.

    But in some functions of the tutorial i'm reading, it looks like this:

    void ComputePositionOffsets(float &fXOffset, float &fYOffset)

    It means that the variable enters as referece too? Or it means something different?
     
  2. MechWarrior

    MechWarrior Master Guru

    Messages:
    378
    Likes Received:
    0
    GPU:
    GTX 780 Classified Sli OC
    When the & operator appears in a function's parameters it just means that the address is passed to the function and not the actual value.

    This is useful because it allows you to in effect write a function that returns more than one value.

    Edit: After re reading your post I think I understand your question better now.

    void someFunction(int& x)
    and
    void someFunction(int &x)
    and
    void someFunction(int&x)

    All mean the same thing.
     
    Last edited: Jul 3, 2012
  3. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Thanks! Thats exactly what i was asking :)
     
  4. gamerk2

    gamerk2 Ancient Guru

    Messages:
    2,108
    Likes Received:
    1
    GPU:
    NVIDIA 570 GTX 1.2 GB
    Yeah, I *think* its a matter of preferred syntax, but I've seen some really strange thinks with C++ operator ordering over the years, so I can't be certain.
     

  5. scatman839

    scatman839 Ancient Guru

    Messages:
    14,121
    Likes Received:
    538
    GPU:
    3080, KD55XD800
    The variable will be the same regardless of where the & symbol is, though I was unaware you could have it either side of the space, I was taught the grammar of doing int &x.

    Some compilers may have a problem with int& x or int&x though.
     
  6. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    Well, i've learned the int& x way :p

    GCC compiles it fine. And keep in mind that the author uses VC++.
     
  7. proliferazor

    proliferazor Master Guru

    Messages:
    684
    Likes Received:
    1
    GPU:
    AMD 7950
    You should know the & symbol has different meanings depending on context.

    First

    int MyFunc(double& param)
    in this case it means a reference. Whatever double you send into this function if you modify it, the double will be changed, param variable IS the whatever double you send into it
    int MyFunc(double param)
    in this case within myfunc param is a COPY of whatever double you send in.

    int& MyFunc( double param )
    this will return a reference to whatever int you returned, usually not a good idea because any variables created within MyFunc will be destroyed at the end and you now have a reference to memory that isn't valid, unless you return a ref to field or global data.

    if ( myBool & myOtherBool ) this is the and operator
    if ( myBool && myOtherBool ) short circuit and operator ( if the bool on the left is false, the other side doesn't execute )

    You can also use pointer references
    int MyFunc(double*& pParam)
    in this case you send it a pointer and within this function param IS the pointer you passed in and modifying it ( such as instantiating new memory )will change the pointer you passed in.

    int MyFunc(double* pParam)
    if in this func you create new memory using this function the pointer you passed in will not point to it.

    You should know that under the hood references use pointers.

    Now the last context is getting the address of something;

    int var = 15; // say you want the address of this which means you need a pointer to it
    int* pVar = &var; // on this line you declare a pointer, and assign it the address of var.
     
  8. proliferazor

    proliferazor Master Guru

    Messages:
    684
    Likes Received:
    1
    GPU:
    AMD 7950
    there is really 4 places to put a pointer asterisk and & ampersand for this I'll use asterisk in my examples you'll see why.
    int* p;
    int * p;
    int *p;
    int*p;

    technically
    int *x; // this is more correct heres why
    //say you want 3 int pointers
    int *x, *y, *z; // looks better than
    int* x, *y, *z;
    // so keeping it on the right would be more appeasing

    You can declare multiple refs too but must be in class and must be assigned in an initializer list
     
    Last edited: Jul 12, 2012
  9. The Chubu

    The Chubu Ancient Guru

    Messages:
    2,537
    Likes Received:
    0
    GPU:
    MSi GTX560 TwinFrozrII OC
    You can use the bitwise and operator with all kind of types right? I mean, its a very low level instruction, pick up two sets of bytes and compare them bit per bit, it shouln't require an specific type as i see it.
     
  10. proliferazor

    proliferazor Master Guru

    Messages:
    684
    Likes Received:
    1
    GPU:
    AMD 7950
    it must evaluate to a boolean expression, but all intergral types can be evaluated as if they were a bool. Since every integral type in c++ is really just a number it will treat any number that is 0 to be false and anything else to true. You should never use floats though because they are never perfectly any number.

    so you can do
    int x = 10000;

    if ( x )
    //do stuff
    in this case the statement will trigger

    since a pointer is just a number repesenting a memory address you can also do

    MyType* pMyPointer = GetSomePointer();
    if ( pMyPointer ) // same as ( pMyPointer != NULL)
    this checks to see if something other than 0 (NULL,nullptr) is assigned to the pointer.

    You can overload these operators on your own types but the function must still return an integral type. Also if you overload the short circuit operator on your own type short circuiting will not work.
     
    Last edited: Jul 13, 2012

  11. proliferazor

    proliferazor Master Guru

    Messages:
    684
    Likes Received:
    1
    GPU:
    AMD 7950
    Oh ya and there is also another && operator that's new in c++11 for move semantics.
    Code:
    //move constructor
    MyClass( MyClass && parameter)
    {
    // you should copy all variables and including the pointers then null the passed in parameter's pointers so it's constructor doesn't delete the allocated dynamic memory that this obj will now own.
    }
    
    //returning and moving using rvalue reference
    MyClass && SomeFunction()
    {
    // you can actually make a local variable here
    MyClass myVariable = MyClass()
    // and return a rvalue reference
    return std::move(myVariable);
    }
    
    what that does is move that data from the obj you made into the variable that you returned.
    
    here is a practical example
    
    std::string && CreateAndReturnString()
    {
    std::string myString = "Hello world";
    return std::move(myString);
    }
    
    if this was just a regular std::string CreateAndReturnString();
    then the string would be created the variables copied, including instantiating new dynamic memory and coping the string over to the new variable. That means that the dynamic memory allocation would happen twice, once when the string variable was assigned and once when the variable was copied.

    With move semantics the dynamic data is not copied. The returned variable will point to the original dynamically allocated string. It becomes the new owner of the data. This is a huge performance optimization.
     
    Last edited: Aug 17, 2012
  12. AllisonGardner

    AllisonGardner Guest

    Messages:
    5
    Likes Received:
    0
    GPU:
    Model
    Great posts.. Helped a lot.
     

Share This Page