Returning a reference to the class? (Chaining)

This is a discussion on Returning a reference to the class? (Chaining) within the C++ Programming forums, part of the General Programming Boards category; I lost my notes when my professor was going over how to "chain" your class by returning a reference to ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Returning a reference to the class? (Chaining)

    I lost my notes when my professor was going over how to "chain" your class by returning a reference to the class somehow (laptop was stolen). Now I can't remember exactly how to return this in the function definition. Any help? See attached shorthand code.

    Code:
    //student.h file> setName function prototype
    class Student{
    public:
    	....
    	Student & setName( const string newName );
    	...
    private:
    	string name;
    	...
    }; // class Student
    
    
    //student.cpp file> setName function definition
    
    Student & Student::setName( const string newName ){
    	name = newName;
    	return ???;
    };
    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    You would return *this;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Thanks a million!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference
    By TheEngineer in forum C Programming
    Replies: 17
    Last Post: 08-12-2009, 11:34 AM
  2. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 12:10 PM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 11:37 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21