www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/12/05/09:15:15

From: rafael AT alpha DOT coe DOT ufrj DOT br (Rafael Gustavo da Cunha Pereira Pinto)
Subject: Re: Pointers to Functions in c++
To: wudavid AT ecf DOT toronto DOT edu (WU DAVID S)
Date: Mon, 5 Dec 94 8:12:05 GMT-3:00
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp Mailing List)

> 
> I was wondering if anyone knew a good method to pass member functions
> to something.
> 
> if I want to develop a generic list of pointers to functions, that say draw_me();
> is there any way to do this?
> What I am doing know is this,
> the list stores two things, the address of the of member (a s a void)
> and an integer identifier.
> e.g. to call, 
> 
> switch (ID)
> case BOX : ( (box *)dummyPointer)->draw(); breajk
> case CIRCLE : ( (circle *)dummyPointer)->draw(); break;
> etc.
> 
> I have heard that C++ functions are just liked dos functions in which the
> first parameter is "this" is this always the case?  Is there a way to
> take advantage of this?


   I don't know what are you doing, but I guess this would help...


1) Create a hierarchy of GraphicObjects:

        +-------+
        |GObject|
        +-------+
        |       |
        +-------+
        |Draw() |
        +-------+
            |
            |
    +-------O----------
    |       |
+------+ +----+
|Box   | |Line|
+------+ +----+
    As you can see, draw is defined as a virtual function in the parent class

2) Now you can do things like this:

    Box b(0,0,10,10);
    Line l(1,1,2,2);

    GObject vector[2];

    vector[1]=b;
    vector[2]=l;
    
    for (int i=0;i<2;i++) vector[i].draw();

3) You can pass the functions as GObject::*, a pointer to a member of GObject


    Hope this helps

        Rafael Pinto


----------------------------------------------------------------------------
Rafael Gustavo C. P. Pinto            | Phone: 55-21-598-2454
Software Engineer - CEPEL             | Fax:   55-21-260-1340
Electric Power Research Center        | 
P.O. Box 2754                         | E-Mail:rafael AT acsi DOT cepel DOT br
Rio de Janeiro, RJ 20.001 - Brasil    |        rafael AT fund DOT cepel DOT br
                                      |        rafael AT coe DOT ufrj DOT br
----------------------------------------------------------------------------

- Raw text -


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