Hi,
In classes, is it a good practice to right the (this) operator for each class member when you need it.
Thank you.
Hi,
In classes, is it a good practice to right the (this) operator for each class member when you need it.
Thank you.
yes, it is.
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):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
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
Qt use "this->" string in 183 .cpp files, and always when temporary variables, parameters and members can have same names.
MIH1406 (3rd June 2010)
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 18:45.
MIH1406 (3rd June 2010)
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.
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...
MIH1406 (3rd June 2010)
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
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.
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
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 "
Bookmarks