I am building a data structure and need to manipulate the object indicated by a pointer. The object indicated by the pointer is:
//in header file
struct treeNode{
int value;
treeNode* left;
treeNode* right;
};
My current implementation only changes what the pointer is pointing to.
target->value = oneLarger->value
Target and oneLarger are defined:
void method(treeNode* target){
treeNode* oneLarger = (tree node retrieved by another method)
target->value = more->value
}
Is this possible to do via pass by reference or do I have to pass by value?
You can do: by Pointer, by Ref, by Copy
note that changes made in the byCopy function will get lost as soon as the function is done!
Those options look like:
//by Pointer
void method(treeNode* target) {
treeNode* oneLarger = ...
target->value = oneLarger->value;
}
//by Ref
// since that is a ref, you access the value using the dot(.) and not the `->` operator
void method(treeNode& target) {
treeNode* oneLarger = ...
target.value = oneLarger->value;
}
//by Copy
// since that is a copy, you access the value using the dot(.) and not the `->` operator
void method(treeNode target) {
treeNode* oneLarger = ...
target.value = oneLarger->value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With