What's the difference between:
void test(int value) {}
void test(int& value) {}
What's the purpose of &?
Passing by reference instead of by value
Why are you programming C++ if you know nothing about it?
>>53361508
Who said I'm programming C++? This is C.
>>53361479
>C++ pointer reference syntax
this shit was mildly convuluted
>>53361545
> This is C.
No it isn't
>>53361580
what makes you say that
syntax seems valid except for int& maybe
What >>53361505 said
Basically, C++ passes things as a copy, not the same object.
& forces the use of the same object
* is a pointer, which is useful if your object is non-copyable or you have other restrictions
>>53361614
This C++, everything is C is pass by value.
>>53361614
>int&
it's C++ style, in C you eould have to pass *int foo and then use *foo = 4 everywhere isntead of foo = 4
>>53361619
pointers are pretty okay for some other minor things too, like polymorphism
>>53361614
int& is C++ not C
And afaik it isnt used like that either
>>53361479
>pointer has two states: value, or address
>three ways to interface with these two states: &ptr, *ptr, or ptr
For what purpose.
>>53361479
there is no bullshit like this in my beloved Javascript (TM)
I would love to know the difference with a simple reminder or cheatsheet.. I'm working with JAVA / C# and has to deal with C++ now. Never, ever, feel so confused. Simple trick anyone?
>>53361685
an expression evaluates to some value
this value can be considered to be a value or the address of a value (it's some integer word sitting somewhere in memory in both cases), and you can look up the address that holds that value
you should probably look it up instead, i make no guarantees of what i'm saying being correct
>>53361685int *ptr, val;
val = 5;
ptr = &val;
printf("%ld\n", ptr); /* address of val */
printf("%ld\n", *ptr); /* value of val * */
printf("%ld\n", &ptr); /* address of ptr */
Hope that helps
It is a cheap thing to save resources.
Assuming 64bit system, a pointer is 64bits.
An int would also be 64bits.
Now if you were to store a vector of ints, the reference is still 64bits, but the value would be a lot bigger.
If you pass int *value instead of int value, you can modify value outside of the scope of the function test()
>>53361753// callee only sees/can mess with copy
void pass_by_copy(int val);
// callee sees original but can't alter it
void pass_by_const_ref(const int &val);
// callee can alter argument
void pass_by_ref(int &val);
// callee can see/alter original, but has to dereference it.
// reassigning address of copied ptr does nothing externally
void pass_by_ptr(int *val) { *val = 42; val++; }
// ref-to-ptr allows deferencing AND internally reassigning
void pass_reassignable_ptr(int *&val) { *val = 99; val = nullptr }
You generally always want to pass by reference (const if possible) except for small (e.g., <=16B) copyable values, which should be passed by value a.k.a. copy.
>>53361644
you can use pointers in C faggot
>>53361906
>An int would also be 64bits.
no
>>53362013
which are values, mr. genius-man
>>53362013
And a pointer is also pass by value.
>>53362014
my bad.
I don't use the standard types very often.
>>53361479
>adress
>>53362132
witsd?
>>53361644
>It's C, except for the C++ part that would make it C++ specific.
Genius.
>>53362243
Not a Cfag, but if I recall correctly C has only pass by value semantics. Pointers are passed by value, the value being an address.
>>53361479
>adress
Jesus christ
>>53362115
Your first mistake was assuming that int has a specific size, at all.
>>53362549
>Assuming anything
>in C
Just ask a bunch of Cfags if a char is always 8 bits and see what happens.
>>53362606
iirc bby default VS makes them always 8 bits in debug mode
>>53362606
B-but muh multi-byte chars
B-but muh ancient exotic arch with 9 bit words
>>53361479
A single & in a type name indicates a reference type. Under the hood, reference types are just pointers. On a syntactic level, however, they add a little bit of extra sugar.
Consider this:#include <iostream>
void foo(int& bar)
{
bar = 6;
}
int main ()
{
int x = 5;
foo(x);
std::cout << x << std::endl;
return 0;
}
This prints out 6. It is equivalent to foo being a function taking in a int* as argument, and doing*bar = 6;
The dereferencing of foo and referencing of x's address is done implicitly.
The advantage of using references is that you can guarantee that your argument can't be null.
>>53362763
A char is always one byte. It is not always 8 bits, but usually is, and it is always at least 8 bits.
>>53365325
>referencing of x's address is done implicitly
Disgusting
>>53365380
> need mandator explicit dereferencing operator or they might forget they're paying an indirection cost
> need separate "struct" namespace so they don't forget their data types and accidentally call-by-value a big argument
C fags, everyone.
>>53362132
sauce?
>>53361644
>This C++, everything is C is pass by value.
Except arrays.
>>53361982
Not the dude who asked but thanks so much for this. C++ is confusing me to no end.
Last year, we were "taught" C++ by a shit teacher, now we're expected to know the language well enough to program mid-sized programs using it.
It's hell :(
>& in this case means reference
>a reference is just a pointer that you syntactically use as if it was passed in.
It means that if you modify i it will be reflected in whatever you passed in as i.
>>53361479
Basically a pointer for people too retarded to understand pointers.