www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "operator" overloading?

reply %u <mail home.com> writes:
Hi everyone,

Was hoping someone could help me make sense of this bit of C++ code:

class canvas
{
    operator HDC() { return _hdc; }
protected:
    canvas(HDC hdc): _hdc(hdc) {}
    HDC _hdc;
}

From what I understand, HDC is an alias for HANDLE in Windows. So
they are overloading canvas such that when assigned to handle
instance it returns _hdc? How is this done in D?

Thanks
Feb 23 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 23.02.2011 17:08, %u wrote:
 Hi everyone,

 Was hoping someone could help me make sense of this bit of C++ code:

 class canvas
 {
      operator HDC() { return _hdc; }
 protected:
      canvas(HDC hdc): _hdc(hdc) {}
      HDC _hdc;
 }

  From what I understand, HDC is an alias for HANDLE in Windows. So
 they are overloading canvas such that when assigned to handle
 instance it returns _hdc? How is this done in D?

 Thanks
Something like this, and i shouldn't be a class in D (it's a proxy sort of, right? ) : struct canvas{ alias getHandle this; this(HDC hdc){ _hdc = hdc; } property HDC getHandle(){ return _hdc; } private: HDC _hdc; } uinttest{ HDC h = cast(HDC)1; canvas c = h;//intitalize h = c; assert(h == c); } -- Dmitry Olshansky
Feb 23 2011
next sibling parent reply Simon <s.d.hammett gmail.com> writes:
On 23/02/2011 14:37, Dmitry Olshansky wrote:
 On 23.02.2011 17:08, %u wrote:
 Hi everyone,

 Was hoping someone could help me make sense of this bit of C++ code:

 class canvas
 {
 operator HDC() { return _hdc; }
 protected:
 canvas(HDC hdc): _hdc(hdc) {}
 HDC _hdc;
 }

 From what I understand, HDC is an alias for HANDLE in Windows. So
 they are overloading canvas such that when assigned to handle
 instance it returns _hdc? How is this done in D?

 Thanks
Something like this, and i shouldn't be a class in D (it's a proxy sort of, right? ) : struct canvas{ alias getHandle this; this(HDC hdc){ _hdc = hdc; } property HDC getHandle(){ return _hdc; } private: HDC _hdc; } uinttest{ HDC h = cast(HDC)1; canvas c = h;//intitalize h = c; assert(h == c); }
That's the wrong way round. The c++ class is a canvas object which can be transparently passed to any windows function which takes a HDC. Note the constructor is protected and the cast operator is public. Not sure of the cast operator details in D though; I've never used it myself. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Feb 23 2011
next sibling parent reply Simon <s.d.hammett gmail.com> writes:
On 23/02/2011 18:42, Simon wrote:
 On 23/02/2011 14:37, Dmitry Olshansky wrote:
 On 23.02.2011 17:08, %u wrote:
 Hi everyone,

 Was hoping someone could help me make sense of this bit of C++ code:

 class canvas
 {
 operator HDC() { return _hdc; }
 protected:
 canvas(HDC hdc): _hdc(hdc) {}
 HDC _hdc;
 }

 From what I understand, HDC is an alias for HANDLE in Windows. So
 they are overloading canvas such that when assigned to handle
 instance it returns _hdc? How is this done in D?

 Thanks
Something like this, and i shouldn't be a class in D (it's a proxy sort of, right? ) : struct canvas{ alias getHandle this; this(HDC hdc){ _hdc = hdc; } property HDC getHandle(){ return _hdc; } private: HDC _hdc; } uinttest{ HDC h = cast(HDC)1; canvas c = h;//intitalize h = c; assert(h == c); }
That's the wrong way round.
Nuts never mind. I'm talking out my ass. Though how do you do it using the cast operator? That would be closer to the c++ implementation. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Feb 23 2011
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 23.02.2011 21:48, Simon wrote:
 On 23/02/2011 18:42, Simon wrote:
 On 23/02/2011 14:37, Dmitry Olshansky wrote:
 On 23.02.2011 17:08, %u wrote:
 Hi everyone,

 Was hoping someone could help me make sense of this bit of C++ code:

 class canvas
 {
 operator HDC() { return _hdc; }
 protected:
 canvas(HDC hdc): _hdc(hdc) {}
 HDC _hdc;
 }

 From what I understand, HDC is an alias for HANDLE in Windows. So
 they are overloading canvas such that when assigned to handle
 instance it returns _hdc? How is this done in D?

 Thanks
Something like this, and i shouldn't be a class in D (it's a proxy sort of, right? ) : struct canvas{ alias getHandle this; this(HDC hdc){ _hdc = hdc; } property HDC getHandle(){ return _hdc; } private: HDC _hdc; } uinttest{ HDC h = cast(HDC)1; canvas c = h;//intitalize h = c; assert(h == c); }
That's the wrong way round.
Nuts never mind. I'm talking out my ass. Though how do you do it using the cast operator? That would be closer to the c++ implementation.
There is no much point in getting closer to the source I think. Well, there sort of was opImplicitCast, but it's functionality is superseded by alias this. When multiple alias this are implemented and some bugs fixed we are sure to witness it's full power. Meanwhile you could just as well make this proxy a template struct: struct proxy(T){ alias getHandle this; this(T handle){ _h = handle; } property T getHandle(){ return _h; } private: T _h; } alias proxy!(HDC) canvas; //and so on and so forth But this sort of thin wrappers are of no good if they do not provide you some benefits like automatic resource management and such. -- Dmitry Olshansky
Feb 23 2011
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, February 23, 2011 10:42:04 Simon wrote:
 On 23/02/2011 14:37, Dmitry Olshansky wrote:
 On 23.02.2011 17:08, %u wrote:
 Hi everyone,
 
 Was hoping someone could help me make sense of this bit of C++ code:
 
 class canvas
 {
 operator HDC() { return _hdc; }
 protected:
 canvas(HDC hdc): _hdc(hdc) {}
 HDC _hdc;
 }
 
 From what I understand, HDC is an alias for HANDLE in Windows. So
 they are overloading canvas such that when assigned to handle
 instance it returns _hdc? How is this done in D?
 
 Thanks
Something like this, and i shouldn't be a class in D (it's a proxy sort of, right? ) : struct canvas{ alias getHandle this; this(HDC hdc){ _hdc = hdc; } property HDC getHandle(){ return _hdc; } private: HDC _hdc; } uinttest{ HDC h = cast(HDC)1; canvas c = h;//intitalize h = c; assert(h == c); }
That's the wrong way round. The c++ class is a canvas object which can be transparently passed to any windows function which takes a HDC. Note the constructor is protected and the cast operator is public. Not sure of the cast operator details in D though; I've never used it myself.
You can overload the cast operator in D: T opCast(T)() {...} but it's for explicit cast only. There is not currently any way to do implicit casts with an overloaded operator. You have to use alias this for that, which may or may not do what be what you want if you want an implicit cast. - Jonathan M Davis
Feb 23 2011
parent Simon <s.d.hammett gmail.com> writes:
On 23/02/2011 19:56, Jonathan M Davis wrote:
 You can overload the cast operator in D:

      T opCast(T)() {...}

 but it's for explicit cast only. There is not currently any way to do implicit
 casts with an overloaded operator. You have to use alias this for that, which
 may or may not do what be what you want if you want an implicit cast.

 - Jonathan M Davis
thanks for the info; and Dmitry. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Feb 23 2011
prev sibling parent %u <nospam home.com> writes:
Thaks to everyone for your assistance.
Feb 23 2011