Results 1 to 19 of 19

Thread: Coding Style for object (this)

  1. #1
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Coding Style for object (this)

    Hi,

    In classes, is it a good practice to right the (this) operator for each class member when you need it.

    Thank you.

  2. #2
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    yes, it is.

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    i don't like writin this constantly with every member. I use it only when it is needed (for example method argument with the same name as class member so you have to you "this->name" to point that you are refering to the member)
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    A (Novice) user said yes.
    An Advanced user said not always.

    But I think it is better to use it always and the new question is "Is it safe to use it always?"

    Thank you all.
    Mohammad

  5. #5
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: Coding Style for object (this)

    Qt use "this->" string in 183 .cpp files, and always when temporary variables, parameters and members can have same names.

  6. The following user says thank you to SABROG for this useful post:

    MIH1406 (3rd June 2010)

  7. #6
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Coding Style for object (this)

    Quote Originally Posted by SABROG View Post
    Qt use "this->" string in 183 .cpp files, and always when temporary variables, parameters and members can have same names.
    Quite interesting statistics. I can guess, how you've found out the number of files, but I cant imagine how you've checked the situations where this pointer is used...

    But I believe these results... I dont remember this pointer somewhere where it's not necessary in Qt sources.

    As far as I know some programmers use this pointer everywhere for quick identifying class members. As for me I prefer using this pointer only if it's necessary and I'm trying to get used to using m_ prefix in class members names. I think it's a good solution.

    Are there any advantages of constantly using this pointer?

    PS
    This topic suites much more "General Programming" thread...
    Last edited by KL-7; 14th September 2009 at 19:45.

  8. The following user says thank you to KL-7 for this useful post:

    MIH1406 (3rd June 2010)

  9. #7
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Coding Style for object (this)

    In my opinion, I don't see no gain from using this pointer everywhere. It just makes it look cluttered up. I've only used it where I had collisions of naming in ctors for example. When I was doing C#. This overly use this pointer everywhere and it was just ugly.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  10. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Coding Style for object (this)

    There is absolutely no difference if you use this or not. (Only in cases of same names variables or function, as mentioned) So if you like to write more and get an in my eyes uglier code: use this->. If you are lazy just use this-> where it is necessary.

    There is no right or wrong. I prefer to write less...

  11. The following user says thank you to Lykurg for this useful post:

    MIH1406 (3rd June 2010)

  12. #9
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    It is clear that there are do any danger on using or not usin the pointer this only if there is same names.

    Thank you
    Mohammad

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coding Style for object (this)

    Quote Originally Posted by Lykurg View Post
    There is absolutely no difference if you use this or not.
    I think there is, actually. I can't provide an example right now, you can look into qlalr examples where this-> is used to cope with calling a non-virtual method from a subclass (as far as I remember).

    Apart from that I see no point in using "this->" in C++ code other than when you are paid by the volume of source code you write.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #11
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Coding Style for object (this)

    on Visual Studio with Visual Assist , "this" let me type my code faster ...

    so if i have a member "myLongMemberName" .. i would type in few clicks

    1. t + enter == this [2 keys]
    2. . == -> (dot == ->) [1 key]
    3. m [1 key]
    4. L [1 key] (hopefully u will see your member here
    5. M [1 key]
    6. N + enter [2keys]

    = 8 keystrokes and if you dont count space and enter then even less

  15. #12
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Coding Style for object (this)

    AFAIK putting this before any member variable would make no dangers in C++, in Javascript it does instead. In js a this abuse would lead to annoying scope issues. ...in js.
    I think that in such cases in C++ hungary notation would be enhough.
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coding Style for object (this)

    Quote Originally Posted by MrDeath View Post
    on Visual Studio with Visual Assist , "this" let me type my code faster ...

    so if i have a member "myLongMemberName" .. i would type in few clicks

    1. t + enter == this [2 keys]
    2. . == -> (dot == ->) [1 key]
    3. m [1 key]
    4. L [1 key] (hopefully u will see your member here
    5. M [1 key]
    6. N + enter [2keys]

    = 8 keystrokes and if you dont count space and enter then even less
    Why can't you skip the first two steps?

    In Qt Creator it's just two steps:
    1. type in "mLMN"
    2. press Ctrl+space

    no "this" required...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #14
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Coding Style for object (this)

    Quote Originally Posted by wysota View Post
    Why can't you skip the first two steps?

    In Qt Creator it's just two steps:
    1. type in "mLMN"
    2. press Ctrl+space

    no "this" required...
    yes we can do the same in VS also...
    "this" is helpful when i want to see the list of all the members in popup,,, i generally dont remember the member names ... so as i type (this)(dot) we have complete list to choose "mLM" from.

  18. #15
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    Since there is no C++ coding conventions, everyone has its solution. Using it only to focus member is a lack of naming convention isn't it?

    Not neccessary except when using polymorphism : omitting it to call a virtual member method will break the virtual mechanism.

  19. #16
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Coding Style for object (this)

    Not neccessary except when using polymorphism : omitting it to call a virtual member method will break the virtual mechanism.
    can you explain this with a example.. i thought "this" has nothing to do with virtual calling... use it or do not use it.. virtual call will always succeed

  20. #17
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: Coding Style for object (this)

    Hi,
    In fact I was wrong.
    The case I was thinking of is the following.

    Qt Code:
    1. #include <iostream>
    2.  
    3. class Base {
    4.  
    5. public:
    6.  
    7. Base() {
    8. this->doSomething();
    9. }
    10.  
    11. virtual ~Base() {
    12.  
    13. };
    14.  
    15. void doSomething() {
    16. this->doSomethingSpecific();
    17. }
    18.  
    19. virtual void doSomethingSpecific() {
    20. std::cout<< "Base::doSomethingSpecific\n";
    21. }
    22. };
    23.  
    24. class Derived : public Base {
    25.  
    26. public:
    27.  
    28. virtual void doSomethingSpecific() {
    29. std::cout<< "Derived::doSomethingSpecific\n";
    30. }
    31. };
    32.  
    33. int main() {
    34.  
    35. Base * test = new Derived;
    36. test->doSomething();
    37. return 0;
    38. }
    To copy to clipboard, switch view to plain text mode 
    and the ouput is :
    Base::doSomethingSpecific
    Derived::doSomethingSpecific
    So you're right. this-> or nothing does exactly the same thing.
    But not always what we might want to do, the call in constructor does not call the overloaded method. That is logical since it has not been allocated yet, because it will be allocated after Base class.

    I had encounter some similar features in working with QWidget but it is another point.

    Sorry for misunderstood.

    S.Cascio

  21. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coding Style for object (this)

    Quote Originally Posted by scascio View Post
    But not always what we might want to do, the call in constructor does not call the overloaded method.
    That's why there is a rule you musn't call virtual methods from constructors and destructors.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. #19
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding Style for object (this)

    Yes. In fact it was a design error.

Similar Threads

  1. QTabWidget transparent background problem
    By destroyar0 in forum Qt Programming
    Replies: 10
    Last Post: 25th June 2009, 13:19
  2. coding style
    By mickey in forum General Discussion
    Replies: 2
    Last Post: 16th March 2008, 19:19
  3. Qt specific questions regaring coding style
    By Raistlin in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 10:14
  4. Coding style about pointer
    By vql in forum General Programming
    Replies: 4
    Last Post: 5th February 2007, 09:07
  5. What is your coding style ??
    By guestgulkan in forum General Discussion
    Replies: 9
    Last Post: 29th May 2006, 10:22

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.