C 如何获取通过该指针调用的函数中的指针地址
我读了,但它没有回答我的问题 文件:hero.hC 如何获取通过该指针调用的函数中的指针地址,c,pointers,C,Pointers,我读了,但它没有回答我的问题 文件:hero.h typedef struct hero { int id; void (*setId)(struct hero*, int); int (*getId)(struct hero*); } Hero, *HeroPtr; void constructHero(HeroPtr this); typedef结构{ int-id; 无效(*setId)(); int(*getId)(); }英雄,*Her
typedef struct hero {
int id;
void (*setId)(struct hero*, int);
int (*getId)(struct hero*);
} Hero, *HeroPtr;
void constructHero(HeroPtr this);
typedef结构{
int-id;
无效(*setId)();
int(*getId)();
}英雄,*HeroPtr;
文件:hero.c
#include "hero.h"
static void setId(HeroPtr this, int id);
static int getId(HeroPtr this);
Hero initObj = {
.setId = &setId,
.getId = &getId,
};
void constructHero(HeroPtr this)
{
*this = initObj;
}
void setId(HeroPtr this, int id) {
HeroPtr hero_obj = this;
hero_obj->id = id;
}
int getId(HeroPtr this) {
HeroPtr hero_obj = this;
return hero_obj->id;
}
#include "hero.h"
#include "stdio.h"
int main() {
Hero hero1;
Hero hero2;
HeroPtr hero_obj1=&hero1;
HeroPtr hero_obj2=&hero2;
constructHero(hero_obj1);
constructHero(hero_obj2);
hero_obj1->setId(hero_obj1, 1);
hero_obj2->setId(hero_obj2, 2);
printf("hero_obj1 id = %d\n", hero_obj1->getId(hero_obj1));
printf("hero_obj2 id = %d\n", hero_obj2->getId(hero_obj2));
return 0;
}
#包括“hero.h”
静态void setId(int id);
英雄obj={
.setId=setId,
.getId=getId,
};
void setId(int id){
HeroPtr hero_obj=0x0;//TODO如何获取地址:hero_obj1(1>)或
//英雄(2>)
英雄炣obj->id=id;
}
void getId(){
HeroPtr hero_obj=0x0;//TODO如何获取地址:hero_obj1(1>)或
//英雄(2>)
返回hero_obj->id;
}
文件:main.c
#include "hero.h"
static void setId(HeroPtr this, int id);
static int getId(HeroPtr this);
Hero initObj = {
.setId = &setId,
.getId = &getId,
};
void constructHero(HeroPtr this)
{
*this = initObj;
}
void setId(HeroPtr this, int id) {
HeroPtr hero_obj = this;
hero_obj->id = id;
}
int getId(HeroPtr this) {
HeroPtr hero_obj = this;
return hero_obj->id;
}
#include "hero.h"
#include "stdio.h"
int main() {
Hero hero1;
Hero hero2;
HeroPtr hero_obj1=&hero1;
HeroPtr hero_obj2=&hero2;
constructHero(hero_obj1);
constructHero(hero_obj2);
hero_obj1->setId(hero_obj1, 1);
hero_obj2->setId(hero_obj2, 2);
printf("hero_obj1 id = %d\n", hero_obj1->getId(hero_obj1));
printf("hero_obj2 id = %d\n", hero_obj2->getId(hero_obj2));
return 0;
}
#包括“hero.h”
int main(){
英雄英雄obj1,英雄obj2;
//1 >
hero_obj1->setId(1);
//2 >
hero_obj2->setId(2);
返回0;
}
看起来您正试图使用函数指针在C中实现虚拟函数。在面向对象的编程语言(如C++或java)中,类内的函数或方法具有隐式<代码>此< /代码>指针作为参数,这是隐藏的。这意味着intgetid()
函数实际上具有签名intgetid(Hero*this)
,而void setId(intid)
函数实际上具有形式void setId(Hero*this,intid)
。正如我已经说过的,在面向对象编程语言中,您不会看到或添加这个指针,并且在调用函数时也不会传递参数。编译器会为您执行此操作。它总是自动将指针作为调用函数的this
指针传递给实例。然而,在C语言中,这些特性并不存在。因此,你必须添加<代码>这个< /C> >参数,并在调用函数时传递它。 < P>你可以做C++后面做的事情。
文件:hero.h
typedef struct hero {
int id;
void (*setId)(struct hero*, int);
int (*getId)(struct hero*);
} Hero, *HeroPtr;
void constructHero(HeroPtr this);
文件:hero.c
#include "hero.h"
static void setId(HeroPtr this, int id);
static int getId(HeroPtr this);
Hero initObj = {
.setId = &setId,
.getId = &getId,
};
void constructHero(HeroPtr this)
{
*this = initObj;
}
void setId(HeroPtr this, int id) {
HeroPtr hero_obj = this;
hero_obj->id = id;
}
int getId(HeroPtr this) {
HeroPtr hero_obj = this;
return hero_obj->id;
}
#include "hero.h"
#include "stdio.h"
int main() {
Hero hero1;
Hero hero2;
HeroPtr hero_obj1=&hero1;
HeroPtr hero_obj2=&hero2;
constructHero(hero_obj1);
constructHero(hero_obj2);
hero_obj1->setId(hero_obj1, 1);
hero_obj2->setId(hero_obj2, 2);
printf("hero_obj1 id = %d\n", hero_obj1->getId(hero_obj1));
printf("hero_obj2 id = %d\n", hero_obj2->getId(hero_obj2));
return 0;
}
文件:main.c
#include "hero.h"
static void setId(HeroPtr this, int id);
static int getId(HeroPtr this);
Hero initObj = {
.setId = &setId,
.getId = &getId,
};
void constructHero(HeroPtr this)
{
*this = initObj;
}
void setId(HeroPtr this, int id) {
HeroPtr hero_obj = this;
hero_obj->id = id;
}
int getId(HeroPtr this) {
HeroPtr hero_obj = this;
return hero_obj->id;
}
#include "hero.h"
#include "stdio.h"
int main() {
Hero hero1;
Hero hero2;
HeroPtr hero_obj1=&hero1;
HeroPtr hero_obj2=&hero2;
constructHero(hero_obj1);
constructHero(hero_obj2);
hero_obj1->setId(hero_obj1, 1);
hero_obj2->setId(hero_obj2, 2);
printf("hero_obj1 id = %d\n", hero_obj1->getId(hero_obj1));
printf("hero_obj2 id = %d\n", hero_obj2->getId(hero_obj2));
return 0;
}
更好地描述你的问题。它编译吗?如果没有,错误是什么?如果是的话,代码怎么会不能正常工作呢?我猜你在寻找c++@OznOg:)不,我只需要使用“c”IMHO,我知道你想做的是不可行的。函数如何知道通过结构中的指针调用它?通常在C语言中,当你模拟面向对象编程时,你会将“对象”明确地传递给函数,因此会是setId(Hero*,intid)
。答案没有改变:你需要自己传递它。这并没有什么诀窍。当编写需要调用成员函数的lambda时,这一点尤其明显,需要捕获此
。如果指针的猜测是可能的,那么就不需要捕捉这个