"this->setEnabled(false);" not always disabling window
-
[quote author="szh1" date="1286314042"]MTK358:
I know, but in C# I learned that it is good practice to do so. I think it is because it makes it clearer if there are other functions with the same name. Is it different in C++?[/quote]Never heard of such a thing (except for languages like Ruby and Python, which require you to do so).
-
szh1: Yes, using this-> makes it clear you are referring to members, but a good IDE does the same thing using syntax highlighting. So why waste time on the extra typing?
MTK358: It is not required, but it makes clear that you want to access a member and not e.g. something global. It is perfect valid C++ and can even be necessary to do to disambiguate between things.
-
VC15: Yes, of course the compiler uses the scoping rules to resolve names. There are places where this->something and something can be different though. Try this:
@
class A {
public:
A() : something(5) {}void method()
{
int something = 7;
qDebug() << something << this->something;
}private:
int something;
};
@ -
I try to avoid such ambiguity. I realize that this is just an example. But in a real project to have a member and a local variable with the same name is not a good practice.
Of course, explicit pointing of scope increases readability. When you use this-> or type fully specified class name with the full chain of namespaces it belongs to you won't be surprised during runtime. But this also bloats your code and sometimes decreases readability. So as usual we have to find some compromise.
-
VC15: I fully agree with all you said.
I do prefer the best practice to avoid variables shadowing others to the best practice of putting "this->" before all member accesses, but both are valid ways to avoid confusing the programmer (not the compiler;-).