typedef void (*T)(void *);在class内部怎么用

_G_M_000 2019-01-04 12:49:16
#include <iostream>
#include <process.h>
#include <windows.h>

struct AAA
{
typedef void (*T)(void *);
void Thread(void *)//禁止使用staitc
{
while (true)
{
std::cout << "+";
Sleep(250);
}
}
void Init()
{
T s = &AAA::Thread;//错误
_beginthread(s, 0, 0);
}
}aaa;
int main()
{
aaa.Init();
getchar();
}

就当我伸手党把,求可执行的代码,实践才是真道理,理论一大堆然后P个代码也写不出来的我不要
...全文
473 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2019-01-09
  • 打赏
  • 举报
回复
只是想用this的话, 一般都这样封装. struct AAA { typedef void (*T)(void *); static void Thread(void * param) { AAA *pThis= (AAA*)param; pThis->Run(); } vitural void Run() { //这里已经是成员 函数, 可以任意的使用this指针了. //这里做线程耗时操作. while(true) { //do something } } void Init() { _beginthread(Thread, 0, this); } }
冷风1023 2019-01-08
  • 打赏
  • 举报
回复
写个我自己的例程,我是在linux下的,不知道对你有没有帮助
class Thread{
typedef void *(*FuncType)(void*);
explicit Thread(FuncType func = 0,void *arg = 0);
........
}

Thread::Thread(FuncType func,void* arg)
{
if (func)
{
pthread_t pid;
int ret = pthread_create(&pid,0,func,arg);
}
}
  • 打赏
  • 举报
回复
引用 8 楼 GM_000 的回复:
楼上各位并没有我需要的答案,不过在此还是很感谢大家的倾情奉献啦。
在本帖子的例子中的Thread总是各种被强制static导致无法直接访问class AAA的成员对象,目前总结得到唯一办法就是把class AAA的指针用各种abcd的办法传递到Thread函数中去,但是static的结果就是Thread其实是唯一的,如果AAA aaa;完后AAA bbb;然后就bug啦,然后消除完bug后这个代码就会变得很不优雅啦,太丑了,惨不忍睹。所以楼主是希望能够消除这个static的,能够直接在Thread里面使用this这个关键词的


这也算个事,不用static方法当然也可以,不要virtual就好,是否static代码都只有一份,bug是你拿aaa操作的缘故。



#include <stdio.h>
#include <process.h>
#include <windows.h>

struct AAA
{
int i = 0;
uintptr_t _handle;

void Init()
{
void (__cdecl AAA::*StartAddr)() = &AAA::Thread;
void **_thread = (void **)&StartAddr;

_handle = _beginthread((_beginthread_proc_type)*_thread, 0, this);
}

void __cdecl Thread();
};

void __cdecl AAA::Thread()
{
while (1)
{
printf("thread #%p -> %d\n", (void *)_handle, i++);
Sleep(200);
}
}

int main()
{
AAA a, b;

a.Init();
Sleep(1000);
b.Init();
getchar();

return 0;
}

棉猴 2019-01-07
  • 打赏
  • 举报
回复
按着6楼提供的方法修改了一下你的代码
#include <iostream>
#include <process.h>
#include <windows.h>

struct AAA
{
int i = 0;
void Thread(void *aaa)
{
AAA* pAAA = (AAA*)aaa;
while (true)
{
std::cout << pAAA->i++;
Sleep(200);
}
}

void Init(void* pParam)
{
typedef void (AAA::*T)(void *);
T t = &AAA::Thread;

typedef void(*T1)(void *);
T1 t1 = *(T1 *)&t;

_beginthread(t1, 0, pParam);
}
}aaa;
int main()
{
AAA aaa;
aaa.Init((void*)&aaa);
getchar();
return 0;
}

以下是运行效果,不知道到是不是你需要的
_G_M_000 2019-01-07
  • 打赏
  • 举报
回复
还有楼主不是很支持C++11的,毕竟C++11也有编译器不支持的时候
_G_M_000 2019-01-07
  • 打赏
  • 举报
回复
#include <iostream>
#include <process.h>
#include <windows.h>

struct AAA
{
	int i = 0;
	void Init()
	{
		_beginthread(Thread, 0, 0);
	}
	static void Thread(void *);
}aaa;
void AAA::Thread(void *)
{
	while (true)
	{
		std::cout << aaa.i++;
		Sleep(200);
	}
}
int main()
{
	aaa.Init();
	getchar();
}
楼上各位并没有我需要的答案,不过在此还是很感谢大家的倾情奉献啦。 在本帖子的例子中的Thread总是各种被强制static导致无法直接访问class AAA的成员对象,目前总结得到唯一办法就是把class AAA的指针用各种abcd的办法传递到Thread函数中去,但是static的结果就是Thread其实是唯一的,如果AAA aaa;完后AAA bbb;然后就bug啦,然后消除完bug后这个代码就会变得很不优雅啦,太丑了,惨不忍睹。所以楼主是希望能够消除这个static的,能够直接在Thread里面使用this这个关键词的
ForgetTomorrow 2019-01-07
  • 打赏
  • 举报
回复
引用 4 楼 GM_000 的回复:
[quote=引用 1 楼 早打大打打核战争 的回复:]
typedef void (*T)(void *);这是声明了函数指针类型T,你要用于类成员函数,需要声明为类成员行数指针类型:
typedef void (AAA::*T)(void *);


引用 2 楼 ForgetTomorrow 的回复:
楼上正解,但即使这样写了_beginthread(s, 0, 0);这句也会报错的,一般都要写成静态函数的。


引用 3 楼 独孤由过 的回复:
可以用C++11的线程

typedef void(Thread::*Functor)();
void start()
{
Functor functor = &Thread::run;
thread = std::thread(functor, this);
}

也可以改成使用函数子,能够适配相同函数特征的函数指针、函数对象、匿名函数等

#include <iostream>
#include <process.h>
#include <windows.h>

struct AAA
{
int i = 0;
void Thread(void *)//禁止使用staitc
{
while (true)
{
std::cout << i++;//无法访问i;
Sleep(200);
}
}
void Init()
{
typedef void (AAA::*T)(void *);
T t = &AAA::Thread;

typedef void(*T1)(void *);
T1 t1 = *(T1 *)&t;

_beginthread(t1, 0, 0);
}
}aaa;
int main()
{
aaa.Init();
getchar();
}

2楼3楼的办法能运行,但是无法访问类成员,Thread好像被强制staitcl,3楼的C++11的线程std::thread可以使用,但是我用不到,win32里面一大堆API都是_beginthread格式的,比如SetWindowsHookEx,CreateWindowEx,WNDCLASS[/quote]
把你要访问的类做为void*参数传进去,函数内强制转换回来就可以访问了。一般都是这样弄的
  • 打赏
  • 举报
回复
个人见解,此句“T1 t1 = *(T1 *)&t;”类似于C++中的向下强制类型转换(视具体编译器而定),尽管最终能生成.exe文件,但一旦执行就会出错!此为该问题的根源吗?
_G_M_000 2019-01-05
  • 打赏
  • 举报
回复
引用 1 楼 早打大打打核战争 的回复:
typedef void (*T)(void *);这是声明了函数指针类型T,你要用于类成员函数,需要声明为类成员行数指针类型: typedef void (AAA::*T)(void *);
引用 2 楼 ForgetTomorrow 的回复:
楼上正解,但即使这样写了_beginthread(s, 0, 0);这句也会报错的,一般都要写成静态函数的。
引用 3 楼 独孤由过 的回复:
可以用C++11的线程

	typedef void(Thread::*Functor)();
	void start()
	{
		Functor functor = &Thread::run;
		thread = std::thread(functor, this);
	}
也可以改成使用函数子,能够适配相同函数特征的函数指针、函数对象、匿名函数等
#include <iostream>
#include <process.h>
#include <windows.h>

struct AAA
{
	int i = 0;
	void Thread(void *)//禁止使用staitc
	{
		while (true)
		{
			std::cout << i++;//无法访问i;
			Sleep(200);
		}
	}
	void Init()
	{
		typedef void (AAA::*T)(void *);
		T t = &AAA::Thread;

		typedef void(*T1)(void *);
		T1 t1 = *(T1 *)&t;

		_beginthread(t1, 0, 0);
	}
}aaa;
int main()
{
	aaa.Init();
	getchar();
}
2楼3楼的办法能运行,但是无法访问类成员,Thread好像被强制staitcl,3楼的C++11的线程std::thread可以使用,但是我用不到,win32里面一大堆API都是_beginthread格式的,比如SetWindowsHookEx,CreateWindowEx,WNDCLASS
独孤过 2019-01-04
  • 打赏
  • 举报
回复
可以用C++11的线程

#include <thread>
#include <chrono>
#include <iostream>

class Thread
{
public:
	Thread(){}
	~Thread()
	{
		stop();
	}
	typedef void(Thread::*Functor)();
	void start()
	{
		Functor functor = &Thread::run;
		thread = std::thread(functor, this);
	}
	void stop()
	{
		if (end)
			return;
		end = true;
		if (thread.joinable())
			thread.join();
	}
private:
	void run()	// 禁止使用staitc
	{
		while (!end)
		{
			std::cout << "+";
			std::this_thread::sleep_for(std::chrono::milliseconds(100));
		}
	}
protected:
	std::thread thread;
	bool end = false;
};

int main()
{
	Thread thread;
	thread.start();
	getchar();
	return 0;
}
也可以改成使用函数子,能够适配相同函数特征的函数指针、函数对象、匿名函数等
ForgetTomorrow 2019-01-04
  • 打赏
  • 举报
回复
楼上正解,但即使这样写了_beginthread(s, 0, 0);这句也会报错的,一般都要写成静态函数的。
  • 打赏
  • 举报
回复
typedef void (*T)(void *);这是声明了函数指针类型T,你要用于类成员函数,需要声明为类成员行数指针类型:
typedef void (AAA::*T)(void *);

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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