URL: https://www.opennet.me/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 2769
[ Назад ]

Исходное сообщение
"нужен массив указателей на функции класса"

Отправлено Lamr , 08-Апр-04 12:09 
массив указат на функ у страунстр хорошо описан, но вот как правильно
объявить его в классе? что-то не выходит, примерчик не подскажите?
Спасибо

Содержание

Сообщения в этом обсуждении
"пример"
Отправлено Lamr , 08-Апр-04 12:47 
Написал такую лабуду
======================

class test {
    int        i;

public:
    test(int i);
    ~test() {}
    void init(int);
    int func1(int);
    int func2(int);
    int (*func[])(int);
};

void
test::init(int i) {
    this->i = i;
    func[0] = &test::func1;
    func[1] = &test::func2;
}
test::test(int i) { init(i); }

int test::func1(int c) { puts("1"); }
int test::func2(int c) { puts("2"); }

main()
{
    test *X = new test(5);
}
//#EOF

А она сволочь ругается
======================
dimfunc.c: In method `void test::init(int)':
dimfunc.c:19: converting from `int (test::*)(int)' to `int (*)(int)'
dimfunc.c:20: converting from `int (test::*)(int)' to `int (*)(int)'

====================
Как объявить-то, бля?


"ЗАРАБОТАЛО! может кому пригодиться? :-))"
Отправлено Lamr , 08-Апр-04 15:17 
#include <stdio.h>

class test {
    int        i;

public:
    test(int i);
    ~test() {}
    void init(int);
    int func1(int);
    int func2(int);
};

int (test::*func[2])(int);

void
test::init(int i)
{
    this->i = i;
    func[0] = &test::func1;
    func[1] = &test::func2;
}

test::test(int i) { init(i); }

int test::func1(int c) { puts("1"); }
int test::func2(int c) { puts("2"); }

main()
{
    test *X = new test(5);

    (X->*func[0])(5);
    (X->*func[1])(5);

}
//#EOF