Thread: Am I using *this properly?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Am I using *this properly?

    I have already written a calculator engine that works equally well in both windows and console mode due to it's IO de-coupled design.

    My goal now is to extend that functionality to a Calculator class along with it's more generic helper classes. The class I am working on now is the StringList class which will track and traverse a list of user input strings.

    Here is where i am unsure. The polymorphic constructor StringList() in one instance will add the StringList object at hand to the single argument, StringList *head. So I am returning *this, but how?


    Code:
    
    class StringList
    {
    
      StringList *next;
    
      char *string;
    
      StringList *prev;
         
    
    StringList * StringList::StringList(int thisManyChars)
     {   next = NULL; string = NewString(thisManyChars);                                                    prev = NULL;  return *this;}
    
    StringList * StringList::StringList(StringList *head, thisManyChars)
      {  string = NewString(thisManyChars);
          StringNode *t = head; while(t->next != NULL) t = t->next;
          prev = t; t->next = this; next = NULL;  return *this;}
     
    StringList * StringList::StringList(StringList *head)
      {  string = NewString(100);
          StringNode *t = head; while(t->next != NULL) t = t->next;
          prev = t; t->next = this; next = NULL;  return *this;}
    
    StringList * StringList::StringList()
                     {next = NULL; string = NewString(100);
                       prev = NULL;  return *this;}
    
    
    
      StringList::~StringList(){ free(string); }
    };
    Am I using *this properly?

  2. #2
    Unregistered
    Guest
    IMO, no. The this pointer refers to the object at hand. In your code in the following line:

    t->next = this;

    the this pointer appears to be refering to the variable called string whcih is of type char *, whereas the object at hand is of type StringList as eviedenced by the function definition:

    StringList * StringList::StringList(StringList *head, thisManyChars)


    Likewise the above definition indicates that the return type will be of type pointer to StringList, but the following line returns a deferenced pointer, rather than the pointer per se.

    return *this;

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    I didn't think you could specify a return type for a constructor.

    The constructor by default return itself!!!

    StringList * StringList::StringList(StringList *head, thisManyChars)

    is not valid..
    it should be:

    StringList::StringList(StringList *head, int thisManyChars)
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. C++ system("rm") not deleting properly?
    By fattysmo in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2008, 11:37 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. How to properly use a .lib file?
    By Kurisu33 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2006, 08:19 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM