Próbuję uchwycić koncepcję funkcji wskaźnika w lepszy sposób. Mam więc bardzo prosty i pracuje jako przykład:wskaźniki funkcji generują "nieprawidłowe użycie funkcji niestałego elementu" błąd
#include <iostream>
using namespace std;
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
int main()
{
int a, b;
int (*plus)(int, int);
int (*minus)(int, int);
plus = &add;
minus = &subtract;
a = operation(7, 5, add);
b = operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
tak daleko tak dobry, Teraz muszę grupy funkcji w klasie, a następnie wybierz dodawanie lub odejmowanie na podstawie wskaźnika funkcji, które używam. Więc po prostu zrobić małą modyfikację jak:
#include <iostream>
using namespace std;
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus, a_minus;
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
a = a_plus.operation(7, 5, plus);
b = a_minus.operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
i oczywistym błędzie jest:
ptrFunc.cpp: In function ‘int main()’:
ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’
ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’
coz ja nie określono, które sprzeciwiają się powołać (i nie chcę użyć metody statyczne na razie)
EDIT: kilka komentarzy i odpowiedzi sugeruje, że wersja non-statyczne (jak pisałem) nie jest możliwe (dzięki dla wszystkich) Więc Mod. ifying klasę w następujący sposób również przyzwyczajenie praca:
#include <iostream>
using namespace std;
class A
{
int res;
public:
A(int choice)
{
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
if(choice == 1)
res = operation(7, 5, plus);
if(choice == 2)
res = operation(20, 2, minus);
cout << "result of operation = " << res;
}
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus(1);
A a_minus(2);
return 0;
}
generowany ten błąd:
ptrFunc.cpp: In constructor ‘A::A(int)’:
ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
może wiem jak rozwiązać ten problem, proszę?
dziękuję
Funkcja członkowska domyślnie przyjmuje "ten" jako pierwszy parametr. –
Musisz je ustawić statycznie. W każdym razie wydają się być naturalnie statyczne. –
Wprowadzę zmiany w moim pytaniu. Proszę spojrzeć, a ja ocenię twoje komentarze.dzięki – rahman