One thing I really dislike in programming is ambiguity with variables names. I'd like to always use the this keyword to access class members, but I often forget to use it in some cases, which leads to some inconsistency.
So I'd like to be forced to use the this keyword when accessing a class member. Would this be a bad idea? I'm thinking there might be a compiler option for that, but I can't find anything about it. I'm using the g++ compiler.
This is a circular problem. You want the compiler to error out and inform you when you're accessing a class member without prefixing this->, so that you cannot be accidentally referring to a local variable or function argument instead … but for that exact same reason, how is the compiler supposed to know that you really intended to access the member? And, if you didn't, how would you access the local variables or function arguments?
C++ is simply not designed this way. Some languages, such as PHP, require that you must use this to access members and any other access is treated as an attempt to read local-scope variables (whether they exist or not), but C++ does not have that. And there is no compiler switch to make it happen. If this worries you, avoid re-using the names of variables!
In short, this is a non-problem that cannot be solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With