Re: ->setTravFuncs

New Message Reply Date view Thread view Subject view Author view

Avi (avi++at++oz.net)
Wed, 14 May 1997 20:31:49 -0700


There are several ways to hack this into reasonably portable C++,
none of which I'd like to promote.

The best (most reliable) way I've found to deal with this is to set
the trav-data to "this" and use a static wrapper callback that just
casts
trav-data back to the appropriate type and calls the member
function of your choice. You can even do this with virtual member
functions (e.g. share a single wrapper callback for an entire class
branch). But I'd recommend making the indirected member function
non-virtual and inlined for the minimum performance penalty.

Avi

P.S. Since callbacks are about the same cost as virtual functions
anyway [e.g., if (callfunc) (*callfunc)(data); is reasonably similar to
(*(_vtbl[f_index]))(data); ], it would have been reasonable to have a
overridable virtual function called pfTravApp(...) or something like
that in pfNode that was called if a pfTravAppFlag was set. I assume
the reason that isn't in there is because of C compatibility and
possibly performance (but that could be dealt with easily enough).

Swaminathan N. wrote:

> On Tue, 13 May 1997, Les Elkins wrote:
>
> > >
> > > I recive the warning:
> > >
> > > warning(3676): pointer-to-member-function cast to pointer
> > > to function
> > >
> > > mv_body->setTravFuncs(PFTRAV_CULL,(pfNodeTravFuncType)walk,NULL);
> > >
> >
> > I just had the same problem a couple of days ago, in that the C++
> > compiler would not let me make a reference to a member function in
> > anything other than an invocation. I assume this is disallowed to
> > make dealing with virtual functions easier. At any rate, I set
> > the traversal _data_ for the node to -this-, and used a callback
> > to a passthrough function to actually call the member function.
> >
> > I would be interested to know why you can't do this as above...
> >
>
> Typically pointers to member functions have a larger size than
> pointers to
> functions. This is because extra data has to be stored if the pointer
> to
> member function is virtual. For instance, if you call such a member
> function
> you get virtual dispatch. Note that there is no difference between
> pointer
> to member function and pointer to virtual member function.
>
> What you actually want is something called a bound pointer,
> &node.callback(),
> which was consciously decided against in the standard. You could hack
> that
> with templates. There's an article by Hicks in the C++ report (also
> reprinted
> in C++ Gems) that deals with precisely this issue.
>
> bye
> Swami
>
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> v Swaminathan Narayanan ^
> v swami++at++evl.uic.edu ^
> v Office: 996-3002 ^
> v Home: 850-2726 ^
> v http://www.evl.uic.edu/swami ^
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>
> =================================================
> =====================
> List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
> Submissions: info-performer++at++sgi.com
> Admin. requests: info-performer-request++at++sgi.com

=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
            Submissions: info-performer++at++sgi.com
        Admin. requests: info-performer-request++at++sgi.com


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:55:13 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.