Thread: Pointer syntax ->

  1. #1
    Registered User
    Join Date
    Mar 2013
    Portugal, Porto.

    Pointer syntax ->

    Hello, I was studying for my exam and came across with a pointer syntax I had never came across with ("this->"). I made some research about it but I didn't really understand it's meaning.
    If I'm correct "return (*this);" would return the object of the class but I have no idea how "this->" is used in the context below in the class constructor.

    class Message {
       friend ostream& operator<<(ostream& os, Message& message);
         time_t date; //time_t is an unsigned integer
         string content;
         Message(time_t date, string content);
         time_t getDate() const;
         string getContent() const;
    Message::Message(time_t date, string content) 
       this->date = date;
       this->content = content;
    I would have probably defined the constructor as follows:

    Message::Message(time_t date, string content) 
       date = date;
       content = content;
    so I don't really understand what is the difference between them.
    Can anyone give me a brief explanation about this subject? Thanks in advance.
    Last edited by Khabz; 06-24-2013 at 09:13 AM.

  2. #2
    Registered User
    Join Date
    Jun 2013
    this is a pointer to the current object of a class. The arrow operator is a syntactic convenience.
    is functionally identical to

  3. #3
    Registered User
    Join Date
    Apr 2006
    this->date will look get the variable "date" that's part of the underling class. Without it, the variable clashes with the like named parameter "date", so your version of the constructor doesn't work.

    Another way to write this constructor follows. This will do the right thing without the explicit "this->".

    Message::Message(time_t date, string content) :date(date),content(content){}
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    I don't know if one should recommend naming parameters in the constructors the same as member variables. Some compilers (like clang, unsure about gcc) will give warnings for this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  2. Help with pointer syntax
    By ajingham in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 07:15 PM
  3. Origin of pointer syntax
    By dudeomanodude in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2008, 01:35 PM
  4. Pointer Syntax
    By monkey_C in forum C Programming
    Replies: 7
    Last Post: 09-13-2002, 03:36 AM