Home » Community » Newbie corner » differences in returns between pointer and reference
Re: differences in returns between pointer and reference [message #56942 is a reply to message #56940] |
Sun, 02 May 2021 18:01 |
Oblivion
Messages: 1094 Registered: August 2007
|
Senior Contributor |
|
|
Hello BetoValle,
In the first instance you are copying the string to another string, not working on a reference.
struct abc{
String b;
public:
abc( String& xx){
b=xx; // <-- the xx variable will be copied to b;
}
void fecha(){
b="retornado"; // this not return: reference constructor is address (No, because you are not working on a reference. You are working on a local variable called "b", which will be destroyed with the struct, by the way.)
}
~abc(){}; // << - Will destroy b.
};
This will work:
struct abc{
String& b; // <-- Now b is a reference.
public:
abc( String& xx)
: b(xx) // > B will refer to xx;
{
}
void fecha(){
b="retornado"; // Will now set the referred object (xx).
}
~abc(){}; // << - Will not destroy what b refers to (xx).
};
[/code]
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sun, 02 May 2021 18:15] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri May 17 00:41:54 CEST 2024
Total time taken to generate the page: 0.02342 seconds
|