delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/25/05:03:27

Date: Tue, 25 Jul 2000 10:06:45 +0200 (WET)
From: Andris Pavenis <pavenis AT lanet DOT lv>
To: Damian Yerrick <d_yerrick AT hotmail DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Pointer to a function in class?
In-Reply-To: <to9qnsgv3rvq44tgv55ghqc3984vrob2vs@4ax.com>
Message-ID: <Pine.A41.4.05.10007251001260.19804-100000@ieva06.lanet.lv>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com


On Tue, 25 Jul 2000, Damian Yerrick wrote:

> On Mon, 24 Jul 2000 19:26:04 GMT, sami3079 AT my-deja DOT com wrote:
> 
> >How do I get a pointer to a function in class?
> 
> To be able to get pointers to class methods in C++, generally you have
> to mark the function pointed to as "static" in the class declaration,
> which means it doesn't get passed a pointer to `this'.
> 
> Seriously, pointers to methods aren't useful unless you're trying to
> interface your C++ code with a library written in C (e.g. Allegro or
> something).  If you're trying to interface C++ with C++, inheritance
> and polymorphism (the `virtual' keyword) provide a cleaner mechanism.
> 

Try following example:

#include <iostream>

class   foo
{
  public:
        int funct1 (void) { cout << "foo::Funct1\n"; }
        int funct2 (void) { cout << "foo::Funct2\n"; }
};

int main (void)
{
        foo     f1;
        int   (foo::*funct[])(void) = { &foo::funct1, &foo::funct2 };
        for (int i=0; i<2; i++) (f1.*funct[i])();
        for (int i=0; i<2; i++) ((&f1)->*funct[i])();
        return 0;
}

And read some good book about C++...

Andris

PS. This is tests example only. If You don't have real application
    for such features, You most likely don't really need it 

    Also this stuff is off topic in DJGPP mailing list. More appropriate
    place would be some C++ related newsgroup
	

- Raw text -


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