delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/17/00:19:22

Message-ID: <319BC174.92@public1.guangzhou.gd.cn>
Date: Fri, 17 May 1996 08:59:48 +0900
From: Wang TianXing <gztxwang AT public1 DOT guangzhou DOT gd DOT cn>
Organization: No Organization
MIME-Version: 1.0
To: GRENDEL AT vm DOT amu DOT edu DOT pl
CC: djgpp AT delorie DOT com
Subject: Re: signatures - what's going on?
References: <17786D55BS86 DOT GRENDEL AT vm DOT amu DOT edu DOT pl>

GRENDEL AT vm DOT amu DOT edu DOT pl wrote:
>
> Hi,
>   I have a signature declared as follows:
>   signature SWindow
>   { // Here should be curly brace but my mailer coverts it to quote
>     void foo();
>     void foo1();
>     ...
>   }
> Now, I understand that the advantage of using signatures is that I don't
> care whether the given class has been derived from the same base as
> another class,as long as they have all the methods specified in the
> signature, is that right? So, having this settled I would like to use
> pointers to signatures, compare the signatures to the NULL value and,
> of course, invoke methods declared in signature - all that without knowing
> what is the _ACTUAL_ class of the object I'm reffering to. But it seems that
> this doesn't work. Whenever I try to do something of the below I get error
> messages from the compiler:
>   // SWindow is the signature defined in header file
>   SWindow*   wnd;
>
>   wnd = NULL; // ERROR
>   if( wnd ) // ERROR
> when, on the other hand I do the following, everything works just fine:

Really?!?

>   // TWindow is a class that conforms to the SWindow declaration
>    SWindow*   wnd;
>    TWindow*   tmp = NULL;
>
>    wnd = tmp;
>    if( (tmp = wnd) )

              ^
I suppose you want to put an == there instead.  But that won't compile.
You cannot compare a signature pointer against anything, including
a signature pointer of the same type.
 
> what is going on? The way I see it signatures are there to eliminate the
> need of such explicit conversions to the specific class, aren't they?
> If signatures allowed assigning NULL to pointers to them, comparison of
> pointers to the NULL value,it would be possible to write a function, for
> example, that would be able to deal with a whole range of classes, not
> necessary derived from the same base. The way the signatures work now
> makes writing such a code useless, 'cause I'd have to incorporate to the
> code specific pointer conversions - and that's what I wanted to avoid.

That happens only when you have to deal with maybe-NULL signature
pointers.  Do you really have to do so?

>   Could someone give me a hint on what's wrong, either with my code or
> with signatures?

It might be better if the compiler supported NULL signature pointers.

I believe g++ stores a 0 for a NULL pointer to any type.  So, the
following workaround should work:

    // anything conforms to SWindow can be used
    // in the place of TWindow:
    const SWindow *SWindow_NULL = (TWindow*)0;

    // to store a NULL signature pointer:
    wnd = SWindow_NULL;  // inelegent.

    // to check whether a signature pointer is NULL:
    if( (void *)wnd ) {  // not-so-inelegent.
        // not NULL
    } else {
        // NULL
    }

Hope this helps.

---
Wang TianXing


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019