Thread: Returning a reference to the class? (Chaining)

  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
    28,413
    You would return *this;
    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
    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, 10:34 AM
  2. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread