This question already has answers here:
gsl::not_null<T*> vs. std::reference_wrapper<T> vs. T&
(2 answers)
Closed 4 years ago.
I have a class that has a reference field that needs to be reassigned. But unlike pointer, it can't be null.
Requirements:
Reference syntax: field.foo() to invoke method, instead of field->foo();
Re-assignable: foo = new_val; // OK
Is it possible to model model this concept in C++?
Yes. Use a std::reference_wrapper<T> instead of a raw reference.
Related
This question already has answers here:
Is it better to pass by value or by reference for basic datatypes? [duplicate]
(4 answers)
Reasons to not pass simple types by reference?
(4 answers)
Is it counter-productive to pass primitive types by reference? [duplicate]
(7 answers)
Closed last month.
When passing objects to functions, we usually do it by constant reference for efficiency reasons, especially with heavy objects, such as an object that contains a huge vector.
Take the following function declaration as an example: void foo(const ObjType &myObj);
Is it a good practice to do a similar thing when passing basic datatypes (int, char, double, etc.) to functions? Does it increase the efficiency or is the difference negligible? Eg: void boo(const int &x);
This question already has answers here:
Why pass by const reference instead of by value?
(8 answers)
Which is faster? Pass by reference vs pass by value C++
(1 answer)
Performance cost of passing by value vs. by reference or by pointer?
(6 answers)
Closed 5 months ago.
When I pass a std::vector into a C++ function, like so:
void myFunc(std::vector<int> myVec)
{
// do something
}
Does the vector myVec get copied or is in reality only a pointer passed? I'm asking from optimization perspective; should I always explicitly pass a pointer to the vector or is there a difference in speed? Assume that I only want to read the data in the function, not manipulate the vector itself.
This question already has answers here:
c++ passing arguments by reference and pointer
(5 answers)
Closed 4 years ago.
Is there any performance difference between passing a pointer and passing a variable by reference? I'm assuming internally they're both using pointers but was wondering if there are any minor differences.
E.g
int v = 5;
by pointer
void MyFunc(int* P);
MyFunc(&v);
or by reference
void MyFunc(int& R);
MyFunc(v);
Both methods will have comparable performance.
Pointers and references are syntactically different but they are identical in terms of runtime performance and generated code in most of the time.
This question already has answers here:
Typedef function pointer?
(6 answers)
Closed 6 years ago.
I ran across a line of code that looks like the following:
typedef Foo* (*CREATE_BAR)(uint32_t);
How exactly does this work? What is happening in this code?
It's a function pointer type named CREATE_BAR which accepts a uint32_t argument and returns a Foo*. It could hold a pointer to any such function.
It is a type for a pointer on function returning Foo*, and taking uint32_t
In c++11, it would be
using CREATE_BAR = Foo* (*)(uint32_t);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what’s the meaning of *&
what * &pSth mean?
is this a pointer or a ref?
Why/When we need that?
--code--
ClassName::GetSth(TypeName* &pSth)
{
//some code
}
It means "reference to pointer to Typename".
TypeName* &pSth is a reference to TypeName pointer.
Equivalent syntax in C is TypeName** pSth