Instance of a class returning itself

This is a discussion on Instance of a class returning itself within the C++ Programming forums, part of the General Programming Boards category; Well, how do I make a function of class return the class it was called for? For some reason I ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    125

    Instance of a class returning itself

    Well, how do I make a function of class return the class it was called for?
    For some reason I have a feeling "return self;" won't work...

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    If you are returning a pointer to the instance, then return this, else if just an object return *this
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Unregistered
    Guest
    you can return a different instance of a class easily. i have a graph class with a function which returns a graph object, different from the graph that was calling it. Is that what you mean?

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    For member functions of a class, use return *this;

    For free functions, like operators, return whichever argument is valid for the next statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Returning reference to STL String class
    By frisbee in forum C++ Programming
    Replies: 5
    Last Post: 12-29-2003, 11:39 AM
  5. returning a temporary class object
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 02:54 PM

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