PDA

View Full Version : Question about class members



serenti
10th June 2009, 18:12
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:


class SomeClass
{
private:
QString userName;
...

and then:


void SomeClass::someFunction(const QString &userName)
{
this->userName = userName;
}

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?

3dch
10th June 2009, 20:00
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.


Quite good: http://www.delta3d.org/filemgmt_data/files/Style_Guide1.1.1.pdf
Arguable: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Variable_Names


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

Regards
Ernst

nish
11th June 2009, 02:51
i prefer some prefix... this helps in naming the function names more easy.

for example


class MyClass
{
QString mUserName;
QString userName(){return mUserName;}//i dont like getUserName()
}

//if you are stuck with notepad then its good to diff between pointers and simple vars..
class MyClass
{
int m_id;
char* mp_name;
}

aamer4yu
11th June 2009, 06:21
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...

Lykurg
11th June 2009, 08:19
Also good is a look to the "official" qt style on their wiki:


http://qt.gitorious.org/qt/pages/QtCodingStyle
http://qt.gitorious.org/qt/pages/CodingConventions

serenti
11th June 2009, 15:20
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:


this->userName = userName;

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:


this->userName = this->userName;

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++ :).