首页
社区
课程
招聘
[求助]菜鸟问题,typedef在编译器中是如何实现的?求大牛帮助!
发表于: 2012-5-13 12:50 6780

[求助]菜鸟问题,typedef在编译器中是如何实现的?求大牛帮助!

2012-5-13 12:50
6780
这个问题可能有些钻死牛角。 但是我确实想知道这是为什么?大牛不要笑我哟
起因,是如下程序:

//typedef.cxx 这个代码是编译不能通过的,因为 编译器不知道 whereItIs到底是个什么东东。
class test
{
public:
        void show(void) {cout<<m_i<<endl;}
        whereItIs m_i;
        typedef int whereItIs;

};

int main()
{
        test o_o;
        o_o.show();
        return 0;
}

下面的两个能编译通过,这个是对比的例子,我都是在show()调用hello函数,而且hello 函数在show函数之下,并不是之前,这是与第一个例子的对比。
//function.cxx
class test
{
public:
        void show(void);
        void hello();

};

void test::show(){
        hello();
}

void test::hello(){
        cout<<"hello world\n";
}

int main()
{
        test o_o;
        o_o.show();
        return 0;
}

//function2.cxx
class test
{
public:
        void show(void){hello();}
        void hello(){cout<<"hello world\n";}

};

int main()
{
        test o_o;
        o_o.show();
        return 0;
}

  我能想到的解释: 第2、3个例子说明编译器在生成代码时,类作用域中的函数符号表是没有先后次序的。 

 我的结论:typedef 改变类型名后,它不会加入符号表。 后面再次验证:确实如此:
1、预处理阶断typedef的类型并没有进行替换,如下图所示:



2、编译阶断也没有whereItIs的符号表


3、汇编阶断同样。

 因此便特别想知道 typedef在编译器中是怎么实现的? 菜鸟

[课程]Android-CTF解题方法汇总!

上传的附件:
  • 1.jpg (10.79kb,153次下载)
  • 2.jpg (25.51kb,154次下载)
收藏
免费 0
支持
分享
最新回复 (6)
雪    币: 273
活跃值: (64)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
2
是不是可以理解为---->编译器 发现关键字 typedef ->然后 确定它的大小后 ,直接生成变量的符号表就ok了,类型是没有符号表的 这样貌似可以
2012-5-13 12:52
0
雪    币: 16
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
到外面派生去
2012-5-13 21:09
0
雪    币: 1708
活跃值: (586)
能力值: ( LV15,RANK:670 )
在线值:
发帖
回帖
粉丝
4
编译是顺序解析源文件的。
这其实很好理解, typedef 对于编译器来说,也是类型定义。
2012-5-14 02:39
0
雪    币: 724
活跃值: (81)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
1.typedef不属于宏的概念,与预处理无关。
2.typedef是给一个类型起一个别名,编译器最终进行类型匹配时,会转成原名进行,因此符号表中不会出现typedef定义的名字,否则类型匹配会更复杂。
3.类型引用需要先定义,后引用,从道理上说,函数也是要先声明,后引用,但对于同属一个类的成员函数而言,可以在声明前引用,如果有你兴趣研究,也许C++标准9.2节点的描述可以解释这个原因:
A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the
class-specifier. Within the class member-specification, the class is regarded as complete within function
bodies, default arguments, exception-specifications, and brace-or-equal-initializers for non-static data members
(including such things in nested classes). Otherwise it is regarded as incomplete within its own class
member-specification.
2012-5-15 15:51
0
雪    币: 273
活跃值: (64)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
6
恩。。前些天 查了C++的标准 。明白了 谢谢 半道出家!
2012-5-16 10:21
0
雪    币: 273
活跃值: (64)
能力值: ( LV12,RANK:210 )
在线值:
发帖
回帖
粉丝
7
换句话说 : typedef 只是为了 遵循让程序员好说, 编译器最终解决让机器能听懂。。
2012-5-16 10:22
0
游客
登录 | 注册 方可回帖
返回
//