Results 1 to 6 of 6

Thread: Question about class members

  1. #1
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Question about class members

    Just wanted to check if there are any cons to what I'm doing. Please feel free to comment.

    When I pass variables to class functions I find it easier and less confusing to use the same names for the variables and for the class members they'll be assigned to, and then just using the this pointer to assign them. So for example, I'll have:

    in class declaration:

    Qt Code:
    1. class SomeClass
    2. {
    3. private:
    4. QString userName;
    5. ...
    To copy to clipboard, switch view to plain text mode 

    and then:

    Qt Code:
    1. void SomeClass::someFunction(const QString &userName)
    2. {
    3. this->userName = userName;
    4. }
    To copy to clipboard, switch view to plain text mode 

    instead of using const QString &aUserName or usrName or nameOfUser or some other modified names, like some sources suggest.

    It works fine, but maybe I'm missing something. Do you see any problem with this?

  2. #2
    Join Date
    Aug 2006
    Location
    Zürich, Switzerland
    Posts
    23
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about class members

    It's no problem for the compiler as you found out yourself.

    But IMHO I'd prefer giving the member variables some prefix as is very common style (examples are _userName, mUserName, m_userName). Besides making code more readable (it won't make me think about the statement, it's quite clear) you have less code to write and won't get in trouble if you ever forget to make the argument non const and at the same time also miss to write the this-> reference (such nonsense can happen when it's late at night or you're in a hurry cause the customer wanted your app days ago...).

    Coding style is an endless story, therefore it's not too bad to get inspired by what already exists and take from that what fits your needs best.



    Should you find a coding style which recommends using your approach than post the link to it here!

    Regards
    Ernst

  3. #3
    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: Question about class members

    i prefer some prefix... this helps in naming the function names more easy.

    for example

    Qt Code:
    1. class MyClass
    2. {
    3. QString mUserName;
    4. QString userName(){return mUserName;}//i dont like getUserName()
    5. }
    6.  
    7. //if you are stuck with notepad then its good to diff between pointers and simple vars..
    8. class MyClass
    9. {
    10. int m_id;
    11. char* mp_name;
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Question about class members

    Try to find what suits you best, u decide what notation to use.
    But once decided... stick to it. Its better to follow one approach consistently rather than changing in between...

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

    Default Re: Question about class members

    Also good is a look to the "official" qt style on their wiki:


  6. #6
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Question about class members

    Thank you for your replies. Actually, I do have a set coding style that I'm happy with, I was just wondering about any problems that OTHER compilers could possibly give me in the future with statements like:

    Qt Code:
    1. this->userName = userName;
    To copy to clipboard, switch view to plain text mode 

    It works fine with VS because obviously it gives the local variables precedence over class variables, so it knows that by "userName" I mean the local variable and not the class variable.

    But is it possible that other compilers could give a different name precedence to variables, so to my surprise I'll find that I'll be assigning the class variable value to the class variable? Meaning, my code will actually become the equivalent of this:

    Qt Code:
    1. this->userName = this->userName;
    To copy to clipboard, switch view to plain text mode 

    I come from programming in Delphi for many years, where there's only one compiler and you don't have to worry about those things. So now I have some of those odd questions about C++ .

Similar Threads

  1. Plugin implementation question
    By JPNaude in forum Qt Programming
    Replies: 12
    Last Post: 27th August 2008, 20:24
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 20:35
  4. Replies: 4
    Last Post: 26th June 2007, 19:19
  5. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00

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.