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 "
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.
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 succeedNot neccessary except when using polymorphism : omitting it to call a virtual member method will break the virtual mechanism.
Hi,
In fact I was wrong.
The case I was thinking of is the following.
and the ouput is :Qt Code:
#include <iostream> class Base { public: Base() { this->doSomething(); } virtual ~Base() { }; void doSomething() { this->doSomethingSpecific(); } virtual void doSomethingSpecific() { std::cout<< "Base::doSomethingSpecific\n"; } }; class Derived : public Base { public: virtual void doSomethingSpecific() { std::cout<< "Derived::doSomethingSpecific\n"; } }; int main() { Base * test = new Derived; test->doSomething(); return 0; }To copy to clipboard, switch view to plain text mode
So you're right. this-> or nothing does exactly the same thing.Base::doSomethingSpecific
Derived::doSomethingSpecific
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
Yes. In fact it was a design error.
Bookmarks