C++类中静态函数如何获得this指针

pww71 2009-06-14 06:22:59
C++类中静态函数如何获得this指针
...全文
1055 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackzhhuang 2009-06-16
  • 打赏
  • 举报
回复
正因为没有this指针,才叫静态函数,如果有……或者说你想有,只能说你类设计有误了。
wbgxx 2009-06-16
  • 打赏
  • 举报
回复
[静态成员函数不存在this指针
DirectX2009 2009-06-16
  • 打赏
  • 举报
回复
学习中···
dl591944556 2009-06-16
  • 打赏
  • 举报
回复
静态成员函数不存在this指针
FreeMyself 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 pww71 的回复:]
需要在类中的静态函数调用该类的一个对象的数据。但是怎么办呢
[/Quote]
作为静态函数的参数传递过去
Zeilone 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 pww71 的回复:]
该类有多个对象 每个对象数据都不同 而静态函数必须针对各个对象数据来运算。所以想用this来区分各个对象
[/Quote]
把对象的地址作或引用作为参数显式传递
ZWHLOVEPRO 2009-06-16
  • 打赏
  • 举报
回复
静态成员函数中没有this指针。
pengzhixi 2009-06-15
  • 打赏
  • 举报
回复
用友元函数
pww71 2009-06-15
  • 打赏
  • 举报
回复
该类有多个对象 每个对象数据都不同 而静态函数必须针对各个对象数据来运算。所以想用this来区分各个对象
pww71 2009-06-15
  • 打赏
  • 举报
回复
需要在类中的静态函数调用该类的一个对象的数据。但是怎么办呢
别逗我乐 2009-06-15
  • 打赏
  • 举报
回复
普通成员函数,有个默认参数,就是this指针!
去掉this指针,形成静态函数的!,它不依赖某个对象的,它是整个类的
pathuang68 2009-06-15
  • 打赏
  • 举报
回复
静态函数没有this指针
kingstarer 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 Proteas 的回复:]
C/C++ code
#include <stdlib.h>

class TestThisPointer
{
public:
TestThisPointer(void):n(0){}
~TestThisPointer(void){}

public:
static void TestStatic(void)
{
unsigned long dwThisPtr = 0;
__asm
{
MOV dwThisPtr, EBX
}

TestThisPointer* thisPtr = (TestThisPointer*)dwThisPtr;
thisPtr->SayHello();
}


[/Quote]

调用静态函数也会修改ex ?
cnnbjwq 2009-06-14
  • 打赏
  • 举报
回复
静态函数属于类的函数,没有this指针,默认的吧在程序中
老邓 2009-06-14
  • 打赏
  • 举报
回复
thunk
vanny 2009-06-14
  • 打赏
  • 举报
回复
静态函数属于类的函数,没有this指针。
amossavez 2009-06-14
  • 打赏
  • 举报
回复
不存在this指针,要不然怎么共享这个指针呀
fanchangyong 2009-06-14
  • 打赏
  • 举报
回复
没有this
Proteas 2009-06-14
  • 打赏
  • 举报
回复

#include <stdlib.h>

class TestThisPointer
{
public:
TestThisPointer(void):n(0){}
~TestThisPointer(void){}

public:
static void TestStatic(void)
{
unsigned long dwThisPtr = 0;
__asm
{
MOV dwThisPtr, EBX
}

TestThisPointer* thisPtr = (TestThisPointer*)dwThisPtr;
thisPtr->SayHello();
}

void SayHello(void)
{
printf("%d:Hello.\n", n);
++n;
}

private:
int n;
};

int main(int argc, char* argv[])
{
TestThisPointer obj;
unsigned long objAddress = (unsigned long)&obj;
unsigned long temp = 0;
__asm
{
MOV EBX, objAddress
}
TestThisPointer::TestStatic();
system("pause");
return 0;
}
fairchild811 2009-06-14
  • 打赏
  • 举报
回复
静态函数只有一个拷贝,不存在this指针。
加载更多回复(4)

33,323

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧