Thread: Referencing function's source

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    Referencing function's source

    Is there any way to reference the source object of a function?
    Code:
    class MyClass
    {
      void SetIt(SomeType Object)
      {
        Object.SomeVariable = the instance of MyClass that this function was called for;
      }
    }
    Something like that.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Loduwijk
    Is there any way to reference the source object of a function?
    Code:
    class MyClass
    {
      void SetIt(SomeType Object)
      {
        Object.SomeVariable = the instance of MyClass that this function was called for;
      }
    }
    Something like that.
    Is this what you mean?
    Code:
    class MyClass
    {
      int SomeOtherVariable
      void SetIt(SomeType& Object)
      {
        Object.SomeVariable = SomeOtherVariable
      }
    }
    Member functions are automatically granted access to member variables of their own object. SomeOtherVariable could be rewritten as this->SomeOtherVariable. "this" is a special keyword, meaning "A Pointer To the Current Object".

    Note the use of the & - Otherwise you will be dealing with a copy of the SomeType object (Which I assume you don't want).
    Last edited by Bench82; 03-19-2006 at 03:08 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    The "this" keyword is what I was after, yes. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  2. Replies: 10
    Last Post: 08-29-2007, 05:59 AM
  3. Mutiple source files for one program
    By earth_angel in forum C Programming
    Replies: 7
    Last Post: 06-08-2005, 09:47 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Source code of the standard library functions...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 12:35 PM