U++ framework
Do not panic. Ask here before giving up.

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 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1284
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


[Updated on: Sun, 02 May 2021 18:15]

Report message to a moderator

 
Read Message
Read Message
Read Message
Previous Topic: gdb debug is not working in theide on macos catalina
Next Topic: static
Goto Forum:
  


Current Time: Fri Oct 09 04:31:51 GMT+2 2026

Total time taken to generate the page: 0.00569 seconds