Is it possible to specify the address returned by the address of operator?

DreadKyller dreadkyller at gmail.com
Thu Sep 28 00:11:56 UTC 2017


On Wednesday, 27 September 2017 at 23:24:58 UTC, user1234 wrote:
> On Wednesday, 27 September 2017 at 21:01:36 UTC, Jesse Phillips 
> wrote:
>> On Wednesday, 27 September 2017 at 16:35:54 UTC, DreadKyller 
>> wrote:
>>> My question is about overloading, several operators can be 
>>> overloaded in D, one of the ones that can't apparently is the 
>>> address of operator (&object). My question is have I simply 
>>> missed it or does it actually not exist, and if it's not 
>>> overloadable, is there any reason why this was decided? 
>>> Because there's been numerous times that it'd be useful to 
>>> me, just recently with how much I use the operator because of 
>>> OpenGL I decided to ask.
>>
>> My answer is that & is a defined operation on all addressable 
>> memory. Unlike other operators which don't exist until you 
>> "overload" them.
>
> Yes but the dereference operator can be overloaded. The 
> reasoning doesn't stand, unless that's recognized as an 
> inconsistency.

Except that no it actually doesn't. The Unary operator * doesn't 
overload the dereferencing of a pointer, take the following code:

class Test
{
	Test opUnary(string s)() if (s == "*")
     {
		writeln("Overloaded operator called");
		return this;
     }
}
void testFunc()
{
	Test test = new Test();
	Test* test_ptr = &test;
	writeln("== *test ==");
	Test other = *test;
	writeln("== *test_ptr ==");
	other = *test_ptr;
	writeln("== end ==");
}

This outputs:

== *test ==
Overloaded operator called
== *test_ptr ==
== end ==

Notice how dereferencing the pointer did not call the overloaded 
function, because a pointer to Test is not the same type as a 
Test.


More information about the Digitalmars-d-learn mailing list