Wskaźnik funkcji do funkcji członka
Chciałbym ustawić wskaźnik funkcji jako członek klasy, która jest wskaźnikiem do innej funkcji w tej samej klasie. Powody, dla których to robię są skomplikowane.
W tym przykładzie chciałbym, aby wyjście było "1"
class A {
public:
int f();
int (*x)();
}
int A::f() {
return 1;
}
int main() {
A a;
a.x = a.f;
printf("%d\n",a.x())
}
Ale to zawodzi przy kompilacji. Dlaczego?
5 answers
Składnia jest błędna. Wskaźnik członka jest inną kategorią typów niż zwykły wskaźnik. Wskaźnik member musi być użyty razem z obiektem klasy:
class A {
public:
int f();
int (A::*x)(); // <- declare by saying what class it is a pointer to
};
int A::f() {
return 1;
}
int main() {
A a;
a.x = &A::f; // use the :: syntax
printf("%d\n",(a.*(a.x))()); // use together with an object of its class
}
a.x
nie mówi jeszcze na jakim obiekcie ma zostać wywołana funkcja. Mówi tylko, że chcesz użyć wskaźnika przechowywanego w obiekcie a
. Prepending a
innym razem jako lewy operand do operatora .*
powie kompilatorowi, na jakim obiekcie ma wywołać funkcję.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-28 14:14:00
int (*x)()
nie jest wskaźnikiem do funkcji członka. Wskaźnik do funkcji member jest zapisywany następująco: int (A::*x)(void) = &A::f;
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-08 15:59:38
Wywołanie funkcji member na poleceniu string
#include <iostream>
#include <string>
class A
{
public:
void call();
private:
void printH();
void command(std::string a, std::string b, void (A::*func)());
};
void A::printH()
{
std::cout<< "H\n";
}
void A::call()
{
command("a","a", &A::printH);
}
void A::command(std::string a, std::string b, void (A::*func)())
{
if(a == b)
{
(this->*func)();
}
}
int main()
{
A a;
a.call();
return 0;
}
Zwróć uwagę na (this->*func)();
i sposób deklarowania wskaźnika funkcji o nazwie klasy void (A::*func)()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:27
Musisz użyć wskaźnika do funkcji członka, a nie tylko wskaźnika do funkcji.
class A {
int f() { return 1; }
public:
int (A::*x)();
A() : x(&A::f) {}
};
int main() {
A a;
std::cout << (a.*a.x)();
return 0;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-08 15:59:25
Chociaż jest to oparte na odpowiedziach sterling gdzie indziej na tej stronie, miałem przypadek użycia, który nie został całkowicie rozwiązany przez nich; dla wektora wskaźników do funkcji wykonaj następujące czynności:
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
class A{
public:
typedef vector<int> (A::*AFunc)(int I1,int I2);
vector<AFunc> FuncList;
inline int Subtract(int I1,int I2){return I1-I2;};
inline int Add(int I1,int I2){return I1+I2;};
...
void Populate();
void ExecuteAll();
};
void A::Populate(){
FuncList.push_back(&A::Subtract);
FuncList.push_back(&A::Add);
...
}
void A::ExecuteAll(){
int In1=1,In2=2,Out=0;
for(size_t FuncId=0;FuncId<FuncList.size();FuncId++){
Out=(this->*FuncList[FuncId])(In1,In2);
printf("Function %ld output %d\n",FuncId,Out);
}
}
int main(){
A Demo;
Demo.Populate();
Demo.ExecuteAll();
return 0;
}
Coś takiego jest przydatne, jeśli piszesz interpreter poleceń z indeksowanymi funkcjami, które muszą być połączone ze składnią parametrów i wskazówkami pomocy itp. Być może również przydatne w menu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-12 22:17:28