[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] array af funktioner, initialisering og kald af dem.



> Et lille sidespørgsmål. Pointere til "løse" funktioner er tilynelaende 
> let, men jeg har et problem med pointere til funktioner i classes.

Ikke saa underligt, det er ogsaa en mærkelig syntaks man skal bruge
(for ikke at sige det er noget uigennemskueligt hø). Et lille eksempel
følger.

HC

PS: Jeg er enig med andre indlæg, at det nok er sjældedent man virkeligt
kan klassificere brug af disse pointere som fint design.

sslug@sslug:/tmp$ g++ -Wall -ansi -o test test.cc
sslug@sslug:/tmp$ ./test
method1: number 1
method2: number 2

------ test.cc --------
#include<iostream>

using namespace std;

class strange {
        int method1 (const string &msg) const {
                cout << "method1: " << msg << endl;
                return 1;
        }

        int method2 (const string &msg) const {
                cout << "method2: " << msg << endl;
                return 2;
        }

        int (strange::*f[2])(const string &msg) const;

public:
        int call (int num, const string &msg) const {
                return (this->*f[num])(msg);
        }

        strange () {
                f[0] = &strange::method1;
                f[1] = &strange::method2;
        }
};

int main (int argc, char *argv[]) {
        strange s;

        s.call(0, "number 1");
        s.call(1, "number 2");
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-03-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *