Thread: Pointer syntax ->

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    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.

    Code:
    class Message {
       friend ostream& operator<<(ostream& os, Message& message);
       private:
         time_t date; //time_t is an unsigned integer
         string content;
       public:
         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:

    Code:
    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.

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