About references in C++.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    About references in C++.

    Hi all. I'm currently learning C++ from a tutorial C to C++. I have a question on references. More precisely, i'm confused why this code gives the following result.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class person
    {
    public:
    
       char *name;
       int age;
    
       person (char *n = "no name", int a = 0)
       {
          name = new char[100];
          strcpy (name, n);
          age = a;
       }
    
       person (person &s)               // The COPY CONSTRUCTOR
       {
          name = new char[100];
          strcpy (name, s.name);
          age = s.age;
       }
    
       ~person ()
       {
          delete [] name;
       }
    };
    
    void foo(person p)
    {
       cout << p.name << ", age " << p.age << endl << endl;
    }
    
    person &bar()
    {
    	return person("Todd", 15);
    }
    
    int main ()
    {
       person k ("John", 56);
       cout << k.name << ", age " << k.age << endl << endl;
    
       foo(k); 
    
       person m(k);
     
       person n = bar();
    	foo (n);
    
       return 0;
    }
    result:
    ------------------------------------------------------
    C:\Documents and Settings\Sebouh >C:\my_cpp_test s\cpp_test\debu g\cpp_test.exe
    John, age 56

    John, age 56

    John, age 4212483 <----------------- WHY??
    -----------------------------------------------------

    why not Todd?


    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your function bar attempts to return a reference to a local variable of type person.
    After functions have returned all local variables have gone and destructed so your
    reference refers to garbage.

    kind regards,

    Jos

    Comment

    • Sebouh
      New Member
      • Feb 2007
      • 77

      #3
      So is it "by chance" that i get John?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Sebouh
        So is it "by chance" that i get John?
        Yep, the memory where that object resided on the stack is gone, it's no more,
        it's pushing up the daisies, it shuffled off its mortal coil, it's gone to meet its
        maker ;-) The content of that memory could be anything.

        kind regards,

        Jos

        Comment

        • Sebouh
          New Member
          • Feb 2007
          • 77

          #5
          Originally posted by JosAH
          Yep, the memory where that object resided on the stack is gone, it's no more,
          it's pushing up the daisies, it shuffled off its mortal coil, it's gone to meet its
          maker ;-) The content of that memory could be anything.

          kind regards,

          Jos
          Heh, alright then.
          Thanks.

          Comment

          Working...