关于typedef的使用

waini12 2004-08-11 05:11:08
比如说我定义了一个这个
typedef char *a;
const a str1; // 请问这条语句的意思是什么?
...全文
191 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
看来你还是没太明白:
typedef char* a; // 这样看更容易理解,意思是:给char*取个别名叫a

typedef void (*PVFN)() ; // 可以这样看:先是void (*PVFN)()是整体,代表一个函数指针,指针名字叫PVFN,返回类型void,参数列表为空。然后用到typedef,直接把PVFN这个名字用作代表void (*)()这一类函数指针的别名,也算一种类型啦。所以,在
PVFN pvfn[] = { func1, func2 };
PVFN摇身一变成了类型。
waini12 2004-08-11
  • 打赏
  • 举报
回复
哦 听了楼上的话我真是茅塞顿开 非常感谢 顺便问一下 怎么结帐呀 谢谢
pacman2000 2004-08-11
  • 打赏
  • 举报
回复
否,是用类似变量定义的方式,用个标识符来表示这个“变量”的类型。
typedef void (*PVFN)() 就是用PVFN表示void (*)()
waini12 2004-08-11
  • 打赏
  • 举报
回复
难道typedef void (*PVFN)() 意思不是说 我可以用(*PVFN)()来代替void吗? 如果是的话那怎么可以把整体中的 PVFN拿出来单独使用呢?这是我不明白typedef的最主要原因?如果不是的话 那PVFN又是个什么类型呢 他好象没有被定义呀!
就象我开始的那个问题一样 typedef char *a 意思不是说可以用*a来代替char吗?为什么在使用的时候只用到了a呢 难道*a不是一个整体吗?
可能问题比较弱智 不过还是希望前辈能够讲清楚 多少分都给
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
pvfn是上面那个以函数指针为类型的数组,
PVFN pvfn[] = { func1, func2 }; // 给数组作初始化
waini12 2004-08-11
  • 打赏
  • 举报
回复
typedef void (*PVFN)();
int main()
{
// Declare an array of pointers to functions.
PVFN pvfn[] = { func1, func2 };

// Invoke one of the functions.
(*pvfn[1])();
}
刚刚这段代码有些不明白了 (*PVFN)() 他是一个函数指针 难道他不是作为一个整体使用的吗? 怎么还可以分开用呢 PVFN pvfn[]=(func1,func2); 请指教
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
你自己看原文吧,不是你理解的意思 :)

我用词不当,不是“劝”,而是解释在该书中为何那样写。
waini12 2004-08-11
  • 打赏
  • 举报
回复
这两种写法是有区别的呀 在不同的场合使用不同呀 怎么说 劝我们用type const而不是const type呢?
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
typedef不是简单的预编译时的字符替换,它虽然是那个类型的别名,但这个时候已经自成一体,就体现在你的例子里。

我上面摘的一段选自《C++ Templates》,大致意思是劝我们用
type const 而不是 const type 的写法。
waini12 2004-08-11
  • 打赏
  • 举报
回复
我想在请问一下 typedef 与 #define 有什么区别 吗?
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
我摘一段给你看看:
We do want to draw your attention to one slightly uncommon decision regarding the declaration of types, parameters, and variables. Clearly, several styles are possible:

void foo (const int &x);
void foo (const int& x);
void foo (int const &x);
void foo (int const& x);
Although it is a bit less common, we decided to use the order int const rather than const int for "constant integer." We have two reasons for this. First, it provides for an easier answer to the question, "What is constant?" It's always what is in front of the const qualifier. Indeed, although

const int N = 100;
is equivalent to

int const N = 100;
there is no equivalent form for

int* const bookmark; // the pointer cannot change, but the
// value pointed to can change
that would place the const qualifier before the pointer operator *. In this example, it is the pointer itself that is constant, not the int to which it points.

Our second reason has to do with a syntactical substitution principle that is very common when dealing with templates. Consider the following two type definitions [1]:

[1] Note that in C++ a type definition defines a "type alias" rather than a new type. For example:

typedef int Length; // define Length as an alias for int
int i = 42;
Lengthl = 88;
i = l; // OK
l = i; // OK

typedef char* CHARS;
typedef CHARS const CPTR; // constant pointer to chars
The meaning of the second declaration is preserved when we textually replace CHARS with what it stands for:

typedef char* const CPTR; // constant pointer to chars
However, if we write const before the type it qualifies, this principle doesn't apply. Indeed, consider the alternative to our first two type definitions presented earlier:

typedef char* CHARS;
typedef const CHARS CPTR; // constant pointer to chars
Textually replacing CHARS results in a type with a different meaning:

typedef const char* CPTR; // pointer to constant chars
The same observation applies to the volatile specifier, of course.

Regarding whitespaces, we decided to put the space between the ampersand and the parameter name:

void foo (int const& x);
By doing this, we emphasize the separation between the parameter type and the parameter name. This is admittedly more confusing for declarations such as

char* a, b;
where, according to the rules inherited from C, a is a pointer but b is an ordinary char. To avoid such confusion, we simply avoid declaring multiple entities in this way.

Lucende 2004-08-11
  • 打赏
  • 举报
回复
msdn是好东西~~

Synonyms for both fundamental and derived types can be defined using the typedef keyword. The following code illustrates the use of typedef:

typedef unsigned char BYTE; // 8-bit unsigned entity.
typedef BYTE * PBYTE; // Pointer to BYTE.

BYTE Ch; // Declare a variable of type BYTE.
PBYTE pbCh; // Declare a pointer to a BYTE
// variable.




The preceding example shows uniform declaration syntax for the fundamental type unsigned char and its derivative type unsigned char *. The typedef construct is also helpful in simplifying declarations. A typedef declaration defines a synonym, not a new, independent type. The following example declares a type name (PVFN) representing a pointer to a function that returns type void. The advantage of this declaration is that, later in the program, an array of these pointers is declared very simply.

// type_names.cpp
// Prototype two functions.
void func1(){};
void func2(){};

// Define PVFN to represent a pointer to a function that
// returns type void.
typedef void (*PVFN)();

int main()
{
// Declare an array of pointers to functions.
PVFN pvfn[] = { func1, func2 };

// Invoke one of the functions.
(*pvfn[1])();
}
qwertasdfg123 2004-08-11
  • 打赏
  • 举报
回复
呵呵!

遭个了!
RookieStar 2004-08-11
  • 打赏
  • 举报
回复
我理解你的意思。这的确是个陷阱。

typedef char *a;
const a str1; // pointer is constant
waini12 2004-08-11
  • 打赏
  • 举报
回复
继续寻找高手回答问题
waini12 2004-08-11
  • 打赏
  • 举报
回复
哈哈 果然找到了一个跟我一样不明白的 我可以坦白的说 楼上的答案是错误的
qwertasdfg123 2004-08-11
  • 打赏
  • 举报
回复
相当于
const char* str1;

typedef 给char*起了一个别名而已。
在以后的使用中,就可以直接使用a来替换char*.

64,439

社区成员

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

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