Programming/Html C++,Visual Basic,HTML,Flash,OpenGL,Modelling,Java.
|
|
|
|
Maha Guru
Videocard: MSi GTX560 TwinFrozrII OC
Processor: i5 2500K stock
Mainboard: Asus P8P67-M Pro
Memory: 16Gb Patriot G2 1333Mhz
Soundcard: Onboard Realtek
PSU: Satellite SL-8600EPS 600w
|
C++, the & operator -
07-03-2012, 18:19
| posts: 2,534 | Location: Look out!
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?
|
|
|
|
|
|
|
|
Master Guru
Videocard: 2XGigabyte GTX580 3Gb SLi
Processor: Intel i7-3960x @ 4.5GHz
Mainboard: MSi X79A-GD65(8D)
Memory: 16 Gig RipjawsX
Soundcard: Onboard for now
PSU: Antec TPQ 1200 watt
|

07-03-2012, 19:19
| posts: 317
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 by MechWarrior; 07-03-2012 at 19:21.
|
|
|
|
|
|
|
|
Maha Guru
Videocard: MSi GTX560 TwinFrozrII OC
Processor: i5 2500K stock
Mainboard: Asus P8P67-M Pro
Memory: 16Gb Patriot G2 1333Mhz
Soundcard: Onboard Realtek
PSU: Satellite SL-8600EPS 600w
|

07-03-2012, 21:30
| posts: 2,534 | Location: Look out!
Quote:
Originally Posted by MechWarrior
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.
|
Thanks! Thats exactly what i was asking
|
|
|
|
|
|
|
|
Maha Guru
Videocard: NVIDIA 570 GTX 1.2 GB
Processor: Intel i7 2600k @ 3.9GHz
Mainboard: ASUS P8P67 Deluxe
Memory: 8GB DDR3 1600 MHz
Soundcard: ASUS Xonar Xense
PSU: 750W
|

07-09-2012, 20:50
| posts: 2,063 | Location: Medford, NY
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.
|
|
|
|
|
|
|
|
Ancient Guru
Videocard: 7970m, UE46D5000
Processor: i7 3610QM
Mainboard: Intel HM77
Memory: 8GB DDR3
Soundcard: Via HD, x-530
PSU: 180w
|

07-09-2012, 21:09
| posts: 8,739 | Location: Dundee, Scotland
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.
|
|
|
|
|
|
|
|
Maha Guru
Videocard: MSi GTX560 TwinFrozrII OC
Processor: i5 2500K stock
Mainboard: Asus P8P67-M Pro
Memory: 16Gb Patriot G2 1333Mhz
Soundcard: Onboard Realtek
PSU: Satellite SL-8600EPS 600w
|

07-11-2012, 00:50
| posts: 2,534 | Location: Look out!
Well, i've learned the int& x way 
GCC compiles it fine. And keep in mind that the author uses VC++.
|
|
|
|
|
|
|
|
Master Guru
Videocard: AMD 7950
Processor: i7 930
Mainboard:
Memory: 12GB DDR3 1600
Soundcard:
PSU: 1KW
|

07-12-2012, 08:47
| posts: 684
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.
|
|
|
|
|
|
|
|
Master Guru
Videocard: AMD 7950
Processor: i7 930
Mainboard:
Memory: 12GB DDR3 1600
Soundcard:
PSU: 1KW
|

07-12-2012, 08:57
| posts: 684
Quote:
Originally Posted by scatman839
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.
|
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 by proliferazor; 07-12-2012 at 19:57.
|
|
|
|
|
|
|
|
Maha Guru
Videocard: MSi GTX560 TwinFrozrII OC
Processor: i5 2500K stock
Mainboard: Asus P8P67-M Pro
Memory: 16Gb Patriot G2 1333Mhz
Soundcard: Onboard Realtek
PSU: Satellite SL-8600EPS 600w
|

07-12-2012, 22:47
| posts: 2,534 | Location: Look out!
Quote:
Originally Posted by proliferazor
if ( myBool & myOtherBool ) this is the and operators
|
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.
|
|
|
|
|
|
|
|
Master Guru
Videocard: AMD 7950
Processor: i7 930
Mainboard:
Memory: 12GB DDR3 1600
Soundcard:
PSU: 1KW
|

07-13-2012, 00:33
| posts: 684
Quote:
Originally Posted by The Chubu
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.
|
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 by proliferazor; 07-13-2012 at 00:50.
|
|
|
|
|
|
|
|
Master Guru
Videocard: AMD 7950
Processor: i7 930
Mainboard:
Memory: 12GB DDR3 1600
Soundcard:
PSU: 1KW
|

07-18-2012, 23:37
| posts: 684
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 by proliferazor; 08-17-2012 at 03:35.
|
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Powered by vBulletin® Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
Copyright (c) 1995-2012, All Rights Reserved. The Guru of 3D, the Hardware Guru, and 3D Guru are trademarks owned by Hilbert Hagedoorn.
|