Thread: return a reference from a class

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    return a reference from a class

    Hello,

    I am coming from C and I understand pointers well enough. But I am having trouble understanding references.

    I have created this simple class.
    Code:
    namespace emp_system
    {
    struct EMP_DETAILS
    {
    	int empID;
    	std::string name;
    };
    
    class Employee
    {
    public:
    	Employee()
    	{
    		_emp_details = new EMP_DETAILS;
    	}		
    	EMP_DETAILS& emp_details();
    private:
    	EMP_DETAILS *_emp_details;	
    };	
    inline
    EMP_DETAILS& Employee::emp_details()
    {
    	return *_emp_details;
    }
    } // emp_system
    And I am using it like this in my main.cpp:
    Code:
    using namespace emp_system;	
    Employee *emp = new Employee;
    	
    emp->emp_details().empID = 10102;
    std::cout << "Emp ID: " << emp->emp_details().empID << std::endl;
    However, I have some questions about this design.
    1) should the struct be part of the class and not outside it. Any benefit to having it this way?
    2) I am not sure I totally understand what I am returning from emp_details. I think it is a reference. Which would be the address of the struct object that is created in the constructor?

    However, this line I think is de-referencing the pointer to get the value.
    Code:
    return *_emp_details;
    However, as I am return the address of (&) would I not need to de-reference and return the pointer instead?
    Code:
    return _emp_details;
    Many thanks,

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    References are like pointers, only without the syntactic difficulty. See them like aliases.
    You pass a variable to another function and it is given another name, but it is still treated like a local variable.
    References hide the "address" so to speak, so you pass them by-value, not their address.

    And putting a struct inside the class has the advantage of it only being visible inside the class scope. So you can make additional structs with the name later without conflicts!
    It also makes it more obvious that the struct is owned by the class!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by steve1_rm View Post
    However, as I am return the address of (&) would I not need to de-reference and return the pointer instead?
    Code:
    return _emp_details;
    Many thanks,
    Since you are most emphatically not returning the address of, you should not dereference. References and addresses are not related, except by the coincidence of sharing a symbol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM