Thread: Help in Understanding object return

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    Help in Understanding object return

    Below is code for main, Prac.h, Student.h, Student.cpp, Prac.cpp
    main
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include<vector>
    #include "Student.h"
    #include "Prac.h"
    
    using namespace std;
    
    int main()
    {
        cout << "Hello world!" << endl;
       // Student h("kunal",89);
        Prac p;
        Student l;
        l= p.test();
        return 0;
    }
    Student.h

    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    #include <iostream>
    #include <string>
    
    using namespace std;
    class Student
    {
        public:
            /** Default constructor */
            Student();
            Student(string t_name, int t_age);
        protected:
        private:
        string name;
        int age;
    
    };
    
    #endif // STUDENT_H
    Student.cpp
    Code:
    #include "Student.h"
    
    Student::Student()
    {
        //ctor
    }
    
    Student::Student(string t_name, int t_age){
         name=t_name;
         age=t_age;
    }
    Prac.h

    Code:
    #ifndef PRAC_H
    #define PRAC_H
    #include "Student.h"
    
    class Prac
    {
        public:
            /** Default constructor */
            Prac();
            Student& test();
    
        protected:
        private:
    };
    
    #endif // PRAC_H
    Prac.cpp
    Code:
    #include "Prac.h"
    
    Prac::Prac()
    {
        //ctor
    }
    Student& Prac::test(){
    
    
        Student p("asdas",90);
    
        cout<<&p;
        return p;
    }
    When I compile program, I get error:
    PATHcode/PlusPlusC/learn/src/Prac.cpp||In member function ‘Student& Prac::test()’:|
    PATH/Documents/lab/code/PlusPlusC/learn/src/Prac.cpp|10|warning: reference to local variable ‘p’ returned [enabled by default]|
    ||=== Build finished: 0 errors, 1 warnings ===|

    When I run program, I get an exception.

    I am going blind in this phase, how to return an object of class.
    when to use * and when to use & while passing and returning.

    Any help shall be highly appreciated in this problem.
    I am new to c++/pointers/reference world.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You problem lies here:
    Code:
    Student& Prac::test(){
    
    
        Student p("asdas",90);
    
        cout<<&p;
        return p;
    }
    p is local to the function, i.e., when the function returns, it is destroyed. Hence, the caller receives a reference to an object that no longer exists.

    You have a few options to fix this, depending on what you are trying to do. For example, you could change the return type of test to Student. This will return a copy of the local variable, hence there is no problem (if that is what you want). Or, maybe p should have been a member variable instead, in which case returning a reference to it is fine because it will still exist after the function returns.

    Quote Originally Posted by deathmetal
    when to use * and when to use & while passing and returning.
    Read up on pointers and references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    laserlight,
    thank you for the help.
    I misunderstood it, as p is passing address of it.
    Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding The Use of Return
    By Unee0x in forum C Programming
    Replies: 2
    Last Post: 03-18-2013, 03:29 AM
  2. Temporary object return
    By jakebird451 in forum C++ Programming
    Replies: 22
    Last Post: 09-01-2011, 08:16 AM
  3. object return type
    By MattJ812 in forum C++ Programming
    Replies: 7
    Last Post: 12-24-2010, 11:23 PM
  4. Replies: 7
    Last Post: 05-22-2010, 05:42 PM
  5. Function doesn't return an object
    By Aiwendil in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2004, 03:15 PM

Tags for this Thread