Re: pointer to a method

From: David White (no.email_at_provided)
Date: 10/24/03


Date: Fri, 24 Oct 2003 22:03:10 +1000


"Woodster" <mirror@127.0.0.1> wrote in message
news:MPG.1a0349792aba9000989681@news.westnet.com.au...
> I was trying to do something similar where I have several derived
> classes each containing it's own specific member variables.
>
> I am trying to construct a report consisting of values extracted from my
> derived classes, where the report is entered as XML and I just extract
> custom tags and replace them with a value. The way I wanted to do this
> was to construct a list of name-value pairs, where the name is the tag I
> am searching for and the value is a pointer to a method from one of the
> derived classes. I do not know which classes the values will be coming
> from until I reach the desired tag.
>
> How would/should I declare a structure that will simply store a string
> value (char *) and a function * as required so I couold call a specific
> class method based on one of these string values?

A pointer-to-member is specific to a single class (the class named in the
pointer's declaration), so you can't have a Base::* pointing to a member of
Derived. However, if all value member functions are declared virtual in
Base, you could do everything with pointers-to-members of Base.

Otherwise, you could associate each name with a proxy object or function,
and the proxy can call the appropriate member function of the appropriate
class of object. For example, the hierarchy proposed in Rolf Magnus's reply
could be used as a proxy hierarchy if it's not suitable as the value
hierarchy itself. Each class's override would have a hard-wired call to a
specific member function of a member object of the appropriate derived
class. That's more trouble than I'd like to go to if I could avoid it,
though.

A cruder, un-OO, approach is to associate each name with an enum. A switch
on the enum would then call the appropriate member function. This would get
messy if an object pointer needed to be kept along with the enum, because
you'd have to cast the pointer to the correct derived-class pointer in the
switch.

DW



Relevant Pages

  • Re: Pointers To Functions in Classes
    ... of setting a function-pointer, you instantiate whichever derived-class ... those functions invoke the method via the base-class, ... >>> The functions used assigned to the pointer and the function pointer ... >> the non-static member you tried to invoke in the first place. ...
    (microsoft.public.vc.language)
  • Re: Casting of pointer to member functions
    ... cast between different types of member function. ... Derived member cannot fit into pointer to Base member. ... You can safely cast pointer to member to other type (which ...
    (microsoft.public.vc.language)
  • Re: can the member function be compared?
    ... So we can set a void pointer's value to be the reference of the member function like below ... I have a question about the pointer ptr. ... Pointer to member is not like regular pointer. ... If you need to distinguish between pointers to member function, then you could make arbitrary index for them. ...
    (microsoft.public.vc.language)
  • Re: IWebBrowser2 ptr returns null, and thus is not able to call navigate ( Migrating from MFC,VC7 to
    ... Probably the pointer is initialized inside DoDataExchange dialog ... member function where dialog controls are bound to respective C++ ...
    (microsoft.public.vc.language)
  • Re: Function Templates with function Pointers ( ...)
    ... >> This is an issue with member function ... >> pointer, rather than a template issue. ...
    (microsoft.public.vc.language)