typedef理解的深的帮忙看一下 谢谢

纯净水o 2018-01-11 03:41:29

如图

我不太明白此时的RunType; 代表什么?

如果去掉图中红框部分,编译就成功了,麻烦大神解释一下。
如果能提供改过的代码就更好了
...全文
305 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2018-01-11
  • 打赏
  • 举报
回复
typedef A RunType则表示RunType类型就是A
paschen 版主 2018-01-11
  • 打赏
  • 举报
回复
typedef A RunType() 中RunType表示的是 返回值为A、参数为空的函数,不能定义函数类型的变量(即不能写:RunType a;),但可以定义指向RunType类型函数的指针,即:RunType* p;表示p是指向返回值为A且参数为空的函数
纯净水o 2018-01-11
  • 打赏
  • 举报
回复
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

typedef void(*R)(void);

void FunDemoR()
{
	printf("FunDemoR\r\n");
}

template <typename A>
void fun(A bbb)
{
	typedef A RunType();
	RunType j2 = FunDemoR;
}

void TypedefStatementFun()
{
	fun(FunDemoR);
}

int _tmain(int argc, _TCHAR* argv[])
{
	TypedefStatementFun();
	return 0;
}

paschen 版主 2018-01-11
  • 打赏
  • 举报
回复
贴下你的代码,等下帮你看下
纯净水o 2018-01-11
  • 打赏
  • 举报
回复
引用 2 楼 jianwen0529 的回复:
就是语法问题。 定义别名,简单理解就是取绰号。 typedef int MyInt; // 使用MyInt类型代表的就是int类型,这里针对类型 typedef void (*Func)(int); //Func是这个函数原型的类型:void (int),这里针对函数/函数指针类型 而你A被定义为模板类型,typedef类型,就相当于A j2 = FunDemoR; 而你传入的 fun(FunDemoR),类型一致,模板代码编译成功
RunType代表啥类型啊
幻夢之葉 2018-01-11
  • 打赏
  • 举报
回复
就是语法问题。 定义别名,简单理解就是取绰号。 typedef int MyInt; // 使用MyInt类型代表的就是int类型,这里针对类型 typedef void (*Func)(int); //Func是这个函数原型的类型:void (int),这里针对函数/函数指针类型 而你A被定义为模板类型,typedef类型,就相当于A j2 = FunDemoR; 而你传入的 fun(FunDemoR),类型一致,模板代码编译成功
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym; The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"

65,187

社区成员

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

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