Like Tree1Likes
  • 1 Post By sonjared

Pointer syntax ->

This is a discussion on Pointer syntax -> within the C++ Programming forums, part of the General Programming Boards category; Hello, I was studying for my exam and came across with a pointer syntax I had never came across with ...

  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.

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    this is a pointer to the current object of a class. The arrow operator is a syntactic convenience.
    Code:
    this->member
    is functionally identical to
    Code:
    (*this).member
    laserlight likes this.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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->".

    Code:
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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, 09:56 AM
  2. Help with pointer syntax
    By ajingham in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 06:15 PM
  3. Origin of pointer syntax
    By dudeomanodude in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2008, 12:35 PM
  4. Pointer Syntax
    By monkey_C in forum C Programming
    Replies: 7
    Last Post: 09-13-2002, 03:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21