[comp.lang.c++] pointers to methods

sho@maxwell.physics.purdue.edu (Sho Kuwamoto) (02/05/90)

I'm not up on 2.0 (did Stroustrup come out with a book yet?  I haven't
been paying much attention to this group lately) but is there any
standard way you can get a pointer to a member function?  How does it
work?  I want something like this:

  funcPtr = className::func;
  instance->funcPtr(arg1, arg2);

Not possible?  Ach so.  What about:

  funcPtr = instance->func;
  funcPtr(arg1, arg2);

Although this version seems harder to implement and uglier to boot.

Now for the big finale.  I'd like something like this, but one which works
for virtual functions.

  class old {                  |       class new : public old {
  public:		       |       public:                 
    virtual void fester();     |         void fester();        
  };			       |       };                      

  funcPtr = old::fester;
  oldThing->funcPtr();
  newThing->funcPtr();

TaDaa!  Another pipe dream.  This would give me more faith that C++
was an object oriented programming language, with messages being
passed around, so forth.  As it is now, I can't shake the feeling that
it's just a big program that mangles names for you.  The only thing
that differentiates C++ from such a mangler program (in my mind) is
virutal functions.  If something like the above is possible (which it
might be), you'd also have containers to put messages in, which you
could then send to different types of objects.

-Sho
--
sho@physics.purdue.edu  <<-- now, all we need is a type variable.  and
                             standard support for generics (or maybe
                             some other more elegant meta-class thing)

roger@decvax.UUCP (Roger H. Scott) (02/06/90)

>[all pointless flaming deleted ...]

IMHO, there are few net practices more irritating than that of posting messages
that start off with "I've been treking in Nepal for the last 3 years, so I'm not
really up on this news group, C++, or life in general, but here are just plain
wrong ideas I have about what's wrong with C++ ..." and proceed to whine at 
length about things that not only have been discussed extensively in this
news group but are also documented in widely available reference works.

randolph@ektools.UUCP (Gary L. Randolph) (02/13/90)

In article <3063@pur-phy> sho@maxwell.physics.purdue.edu.UUCP (Sho Kuwamoto) writes:
*I'm not up on 2.0 (did Stroustrup come out with a book yet?  I haven't

He is doing his best to meet this year's deadline.

*been paying much attention to this group lately) but is there any
*standard way you can get a pointer to a member function?  How does it
*work?  I want something like this:
*  funcPtr = className::func;
*  instance->funcPtr(arg1, arg2);

Not a problem.  
The definition of funcPtr will look like:

   retType (className::*funcPtr)(argType, argType);

Then the actual call:

   instance->*funcPtr(arg1,arg2);

This is documented in Lippman, pg. 213 and in 1988 USENIX proceedings
pg. 305.

*Now for the big finale.  I'd like something like this, but one which works
*for virtual functions.

Also possible. See USENIX '88 referenced above.

*TaDaa!  Another pipe dream.  This would give me more faith that C++
*was an object oriented programming language, with messages being
*passed around, so forth.  As it is now, I can't shake the feeling that
*it's just a big program that mangles names for you. 
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is, if you don't know how to use it.

   Gary

These views are mine and are not meant to represent views of the Eastman 
Kodak Company.