This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing a pointer by reference in c++
I have a function that needs to modify a pointer, ex:
bool someFunc(Something* something)
{
something = somethingElse;
return true;
}
The pointer is passed by value through and is not modified. How can I modify it?
Thanks
Just change the function signature to look like
bool someFunc(Something* &something)
and you'll get a modifiable pointer in someFunc().
bool someFunc(Something * &something)
bool someFunc ( Something * & something );
// ^ notice the reference symbol
Related
This question already has answers here:
What does void* mean and how to use it?
(10 answers)
Closed 1 year ago.
If void* makes a pointer that holds the address of any type, what does adding void* to the start of the function mean, why not just put void?
void* myFunction(void* arg)
{
int thread_id = *((int*) arg);
void *myFunction() means that myFunction returns "pointer to void", i.e. it returns a pointer, for which the type pointed to is not specified.
void myFunction() means that myFunction returns "void", i.e. it returns nothing at all.
This question already has answers here:
Does a const reference class member prolong the life of a temporary?
(6 answers)
Closed 3 years ago.
I'm reviewing code and which looks kind of wrong to me.
I know it can be written in another way and I know it is probably useless to write it this way.
Still, I was surprised that the compiler didn't generate any error/warning so I wonder if it is legal and why.
struct A
{
int val = 0;
int* GetVal() {
return &val;
}
};
void main()
{
A a;
int* const& r = a.GetVal();
}
AFAIK, a reference represents a real variable. The reference and the variable should both have the same memory address.
In this example, there is no variable holding the address (maybe a temporary?) so which variable does r refer to?
If I remove the const it doesn't compile.
There's one special case with references: a const reference is allowed to be bound to a temporary object, and it extends the lifetime of that temporary. Non-const references can't do this magic.
In your code, GetVal() returns a pointer by value, that is it returns a temporary pointer object. When used as a const reference initializer, the compiler stores that pointer value somewhere (most likely in the current stack frame) and binds the reference to that location.
This question already has answers here:
Function References
(5 answers)
Closed 8 years ago.
When to use function reference such as
void (&fr)() = foo;
fr();
instead of function pointer such as
void (*fp)() = &foo;
fp();
Is there something function pointer can't do but function reference can?
when you define a reference:
void (&fr)() = foo;
fr();
it gives you the ability to use fr almost everywhere where it would be possible to use foo, which is the reason why:
fr();
(*fr)();
works exactly the same way as using the foo directly:
foo();
(*foo)();
One more Difference is dereferencing a function reference doesn't result in an error where function pointer does not require dereferencing.
This question already has an answer here:
What is Node *& aNode? [duplicate]
(1 answer)
Closed 10 years ago.
I don't know much about C++ and I need to deal with a function at the moment. What does this mean in a function prototype?
void myFunc(int &size, signed char *&array);
This is a reference to a pointer. So you don't pass a copy of the pointer to the function, instead you pass a reference to it, which means any changes to that value in the function would actually affect the original pointer you passed as argument.
It means array is a reference to a signed char *, i.e. to pointer.
*& is a reference to a pointer
You suppose to call myFunc like this:
int size;
signed char *p;
myFunc(size, p);
Normally pass a reference to pointer to a function, so could change the pointer inside the function.
think of it in this way:
(int) &size;
(char*) &array;
This question already has answers here:
Default value to a parameter while passing by reference in C++
(17 answers)
Closed 8 years ago.
The following function declaration:
void Foo:DoSomething( ULONG &Count = 0) { .... }
Results in the following compile time error
error C2440: default argument cannot convert from 'int' to 'ULONG &'
What is the correct way of creating the signature so that when there is no parameter provided for Count its value will be zero.
You're taking a non-const reference to Count, so it can't be assigned by default with r-value.
Use
void Foo:DoSomething( const ULONG &Count = 0) { .... }
void Foo:DoSomething( ULONG Count = 0) { .... }
You can't do this for non-const references. A reference is pointing to an address in memory. Unless you specifically need reference semantics, I'd recommend just passing by value.