www.digitalmars.com         C & C++   DMDScript  

D - this * for struct methods versus inout this

reply Burton Radons <loth users.sourceforge.net> writes:
When in a struct method, the "this" is a pointer to the struct.  It'd be 
better for language leveraging, some clumsy syntax with operator 
overloading, and consistent syntax if "this" were an inout reference 
instead.
Jul 07 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Why would you ever want a class method to be able to change its 'this'
pointer?  C++ makes this const for a good reason.

Sean

"Burton Radons" <loth users.sourceforge.net> wrote in message
news:bedj00$20vt$1 digitaldaemon.com...
 When in a struct method, the "this" is a pointer to the struct.  It'd be
 better for language leveraging, some clumsy syntax with operator
 overloading, and consistent syntax if "this" were an inout reference
 instead.
Jul 08 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Sean L. Palmer wrote:
 Why would you ever want a class method to be able to change its 'this'
 pointer?  C++ makes this const for a good reason.
I'm not talking about "this" in class, only about struct. This is currently how it's done: struct vec3 { vec3 mulass (vec3 other) { return *this = *this * other; } } Pointers can be more than one cell in length, can be null, and can be re-assigned. "this" in struct is invalid on all counts: void foo () { vec3 bar; if (this === null) /* Never true */ this [1]; /* Invalid */ this = &bar; /* Invalid */ }
Jul 08 2003
next sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
You're saying it should be a reference instead of a pointer.

I agree.

Sean

"Burton Radons" <loth users.sourceforge.net> wrote in message
news:befel6$qo1$1 digitaldaemon.com...
 Sean L. Palmer wrote:
 Why would you ever want a class method to be able to change its 'this'
 pointer?  C++ makes this const for a good reason.
I'm not talking about "this" in class, only about struct. This is currently how it's done: struct vec3 { vec3 mulass (vec3 other) { return *this = *this * other; } } Pointers can be more than one cell in length, can be null, and can be re-assigned. "this" in struct is invalid on all counts: void foo () { vec3 bar; if (this === null) /* Never true */ this [1]; /* Invalid */ this = &bar; /* Invalid */ }
Jul 10 2003
prev sibling parent Farmer <itsFarmer. freenet.de> writes:
Burton Radons <loth users.sourceforge.net> wrote in 
news:befel6$qo1$1 digitaldaemon.com:

 Pointers can be more than one cell in length, can be null, and can be 
 re-assigned.  "this" in struct is invalid on all counts:
 
     void foo ()
     {
        vec3 bar;
        if (this === null) /* Never true */
        this [1]; /* Invalid */
        this = &bar; /* Invalid */
     }
 
Actually, "this" can be null, especially since Walter allowed allocation of structs with new. Of course, I agree that making "this" should be an inout reference. Farmer.
Jul 28 2003