Results 1 to 4 of 4

Thread: this pointer

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default this pointer

    Below is the code and the error ,i am not able to get the reason for it,this is not in anyway necessary but it looks good and effiecient and above all now i have a oppurtunity to see how much can it misbehave so i am using it

    View
    Qt Code:
    1. void height(int heightValue)
    2. {
    3. myScene->height(heightValue);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Scene
    Qt Code:
    1. private:
    2. int heightValue;
    3.  
    4. void height(int heightValue)
    5. {
    6. this.heightValue = heightValue;
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: this pointer

    Where is the error ? I cant see it

    By the way.. this is a pointer.. so you cant use dot operator with it.

    you have to use something like this->

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: this pointer

    Where is the error? this->heightValue = heightValue is perfectly acceptable code and used to define the scope of the variable (However, personally I would use m_heightValue rather than this->heightValue)

    and, this is a pointer, so you need to use -> not .

  4. #4
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Red face Re: this pointer

    As previously pointed out:

    this is a pointer, and thus you have to dereference it with -> or (*) before using
    it in the manner in which you're trying to do. Also, it's best to have private member variables
    (m_) for classes with accessors and mutators so that you can maintain strict controls on who is
    able to modify them. It might not be an issue for a simple test application, but it's good practice
    to learn for coding larger projects with several people
    Also, if you're looking for bad usages of this, some people feel that usage of this in the constructor is bad practice, as you're referencing an object that isn't fully formed.

Similar Threads

  1. this pointer
    By freekill in forum Newbie
    Replies: 10
    Last Post: 30th January 2010, 01:17
  2. Pointer to Pointer
    By ^NyAw^ in forum General Programming
    Replies: 5
    Last Post: 25th April 2009, 08:00
  3. pointer and iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 3rd February 2008, 22:24
  4. pointer at QMainWindow
    By mattia in forum Newbie
    Replies: 10
    Last Post: 7th November 2007, 10:34
  5. pointer
    By mickey in forum General Programming
    Replies: 4
    Last Post: 1st September 2006, 00:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.