3 ms·
Agreed, one of the biggest design mistakes in the OOP syntax of C++ (and Java, for that matter) was not making `this` mandatory when referring to instance membe
by MontyCarloHall 1y ago
Agreed, one of the biggest design mistakes in the OOP syntax of C++ (and Java, for that matter) was not making `this` mandatory when referring to instance members.
- chuckadams 1y agoC++ and Java went for the "objects as static closures" route, where it doesn't make any sense to have a `this`. Or, they made them superficially look like static closures, which in hindsight was probably not the best idea. Anyway, Java lets you use explicit `this`, I don't recall whether C++ makes it into a footgun or not.
- MontyCarloHall 1y agoBoth languages let you use explicit `this` but don’t mandate it. The “static closure” approach is great. I don’t like having to explicitly pass `this` as a parameter to every method call as in the OP (or worse, the confusing Python approach of forcing `self` to be explicitly written in every non-static method signature but having it be implicitly passed during method calls). What I don’t like is being able to reference instance members without `this`, e.g. void foo() { int x = bar + 1; // should be illegal: it can be hard to distinguish if `bar` is a local variable versus an instance member int y = this->bar + 1; // disambiguation is good }
- josefx 1y ago> int x = bar + 1; // should be illegal: it can be hard to distinguish if `bar` is a local variable versus an instance member If it was this->bar it could be a member, it could also be a static variable. A bar on its own could be local or it could be in any of the enclosing scopes or namespaces. Forcing "this" to be explicit doesn't make the code any clearer on its own.
- ryao 1y agoThe guy was referring to the explicit case where bar is a member variable. The cases where it is in the local scope under scoping rules or the global scope are not really an issue, since you can check the function definition to find if it is local. In the case that it is not in the function definition, then it is in the global scope in C. If implicit this were not done in C++, that would also be the case in C++, provided you do the sane thing and use the std namespace for everything. Just thinking about the headaches namespaces could cause when looking for such definitions with cscope gives me yet another reason to stay away from C++ whenever possible.
- cherryteastain 1y agothis in C++ is just a regular pointer, it has no special footguns, just the typical ones you have with pointers in general
- jcelerier 1y agothat's not really true - unlike a regular pointer, `this` is not allowed to be null, thus removing `if(this == nullptr)` is always a valid optimization to do.
- hhdknddkkjd 1y agoOnly in so much that this being null is UB, but in the real world it very much can be null and programs will sometimes crash deep inside methods called on nullptr.
- cherryteastain 1y agoIt absolutely is allowed to be null: #include <iostream> struct Foo { void bar() { std::cout << this << std::endl; } }; int main() { Foo *p = nullptr; p->bar(); } will print 0
- Krssst 1y agoThis is undefined behavior in my understanding, it just happens to work until it doesn't. I wouldn't be surprised if any null check against this would be erased by the optimizer for example as the parent comment mentioned. Sanitizers might check for null this too.
- usefulcat 1y agoFoo *p = nullptr; p->bar(); That's undefined behavior. It's "allowed" in the sense of "yes it's possible to write, compile and run that code", but the language makes no guarantees about what the results will be. So maybe not the most useful definition of "allowed".
- steveklabnik 1y ago
- ryao 1y agoSupporting an explicit this is required to be able to access the member variable when a local variable hides it under scoping rules. As another person replied, C++ does indeed have an implicit this.
- cjfd 1y agoMandatory this can also be a major hit in readability. What if you have a class that implements the abc-formula. You get (- this->b + sqrt(this->b * this->b - 4 this->a * this->c))/(2 * this->a) and (- this->b - sqrt(this->b * this->b - 4 this->a * this->c))/(2 * this->a) This is a readability problem for any class that is used to do computations.
- eps 1y agoYou must be joking. That would be a complete redability disaster... at least for C++. Java peeps probably won't even flinch ;)