Thread: Class function that returns the instance it's called from

  1. #1
    L Boksha
    Guest

    Class function that returns the instance it's called from

    I have a (sorta) recursive function that searches through a linked list of instances of a class. At a certain point, this recursive function will find that the instance it is called for currently is the instance that matches the search criteria. Now I'd like to know; how would I return that instance?
    (very stripped down: )
    Code:
    class AClass
    {
      public:
        AClass *search(int a);
        AClass *next;
      private:
        int A;
    };
    
    AClass *AClass::search(int a)
    {
      if (a==A)
        return huh;
      else
        return next->search(a);
    }
    Now, what goes in the place of huh?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What do you need to return? Whether to value was found? The position in the list of the found value? If you just need the instance you could return 'this'.

    If it's just a bog standard list you probably shouldn't be using a recursive solution.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    this? That should work. Thanks for the fast reply!
    But what's a bog standard list? Any faster solutions?
    Would &this work? The function should return a pointer to the instance where a=A.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    A list that only contains nodes that points to one of their previous or next nodes. If this is the case you'd be better of iterating through it until you reached your NULL or sentinel nodes.

    'this' is a pointer.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    OK, I guess I'll iterate through 'em then. Thanks for clearing that up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM