digitalmars.D.learn - Get the address of an object, within the object itself
- Andrew Chapman (15/15) Feb 15 2017 Hi all, sorry if this question is silly, but is it possible to
- Jonathan M Davis via Digitalmars-d-learn (5/17) Feb 15 2017 This does _not_ give you the address of the Node object. It gives you th...
- Jonathan M Davis via Digitalmars-d-learn (6/29) Feb 15 2017 IIRC, the only way to get the address of the object itself would be to c...
- Andrew Chapman (6/36) Feb 15 2017 Thanks Jonathan. Good point about the reference address. I can
- Jacob Carlborg (16/19) Feb 16 2017 If it's only for printing you can use the C "printf" without any casting...
Hi all, sorry if this question is silly, but is it possible to get the address of an object within the object itself? e.g. class Node { this() { writeln(&this); // Doesn't work } } auto node = new Node(); writeln(&node); // Does work Thanks very much, Cheers, Andrew.
Feb 15 2017
On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- learn wrote:Hi all, sorry if this question is silly, but is it possible to get the address of an object within the object itself? e.g. class Node { this() { writeln(&this); // Doesn't work } } auto node = new Node(); writeln(&node); // Does workThis does _not_ give you the address of the Node object. It gives you the address of the reference. - Jonathan M Davis
Feb 15 2017
On Wednesday, February 15, 2017 13:33:23 Jonathan M Davis via Digitalmars-d- learn wrote:On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- learn wrote:IIRC, the only way to get the address of the object itself would be to cast it to void*, but it's not something that I do normally, so I'd have to experiment a bit to be sure. - Jonathan M DavisHi all, sorry if this question is silly, but is it possible to get the address of an object within the object itself? e.g. class Node { this() { writeln(&this); // Doesn't work } } auto node = new Node(); writeln(&node); // Does workThis does _not_ give you the address of the Node object. It gives you the address of the reference.
Feb 15 2017
On Wednesday, 15 February 2017 at 21:37:12 UTC, Jonathan M Davis wrote:On Wednesday, February 15, 2017 13:33:23 Jonathan M Davis via Digitalmars-d- learn wrote:Thanks Jonathan. Good point about the reference address. I can work around this quite easily, but I was curious. I will try the void* cast and see what happens. Cheers.On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- learn wrote:IIRC, the only way to get the address of the object itself would be to cast it to void*, but it's not something that I do normally, so I'd have to experiment a bit to be sure. - Jonathan M DavisHi all, sorry if this question is silly, but is it possible to get the address of an object within the object itself? e.g. class Node { this() { writeln(&this); // Doesn't work } } auto node = new Node(); writeln(&node); // Does workThis does _not_ give you the address of the Node object. It gives you the address of the reference.
Feb 15 2017
On 2017-02-15 22:42, Andrew Chapman wrote:Thanks Jonathan. Good point about the reference address. I can work around this quite easily, but I was curious. I will try the void* cast and see what happens.If it's only for printing you can use the C "printf" without any casting: import core.stdc.stdio; class Node { this() { printf("%p\n", this); } } void main() { new Node; } -- /Jacob Carlborg
Feb 16 2017