编译器如何了解const成员函数 [英] How compiler know about const member function

查看:88
本文介绍了编译器如何了解const成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

编译器如何处理const成员函数.它是否使任何包含有关该类的const成员变量信息的表/变量.

请提供一些参考书/链接以获取详细知识.

等待您的宝贵建议.


问候,
Narayan

Hi All,

How compiler treat const member function. does it make any table/variable that hold info about const member variable of that class.

Please suggest some reference book/link for detail knowledge.

Waiting for your valuable suggestion.


Regards,
Narayan

推荐答案

将const添加到成员函数时,您正在执行的操作是告诉编译器拒绝代码,如果您尝试修改对象中的任何数据(有奇怪的例外,例如可变的).因此,编译器必须进行一些检查,以查看该函数确实没有进行任何修改.

与虚函数,异常处理和运行时类型信息不同,const是永远不会使其脱离编译器后端的那些事情之一[1].编译器不会添加代码来增强恒定性",它只是在编译时进行检查,并且如果代码违反了规则,它将拒绝它.编译[1]之后,目标代码中没有记录函数的const-这就是为什么您可以在运行时使用指针在C ++中做令人作呕的事情.

只是要添加到SA的上述参考文献中,如果可以找到副本,请阅读Stanley Lippman的在C ++对象模型内部.它涵盖了到达最终机器代码的程序实体(例如虚拟函数,RTTI)以及在编译过程中转储的内容(例如类,数据成员的属性,访问说明符).

[1]严格在链接之后-在此之前,您可能可以从其修饰名称中得出函数的const.如果这样做,那么您手上的时间就太多了!
When you add const to a member function what you''re doing is telling the compiler to reject your code if you try and modify any data in the object (with the odd exception, e.g. mutable). So the compiler has to do a bit of checking to see that the function really doesn''t modify anything.

Unlike virtual functions, exception handling and runtime type information, const is one of those things that never makes it out of the back-end of the compiler [1]. The compiler doesn''t add code to enforce "constness," it just checks as it compiles and if the code breaks the rules it rejects it. After compilation [1] there''s no record in the object code that a function''s const - and this is why you can do disgusting things in C++ with pointers at runtime.

Just to add to SA''s references above, if you can find a copy, have a read of Inside the C++ object model by Stanley Lippman. It covers the program entities that make it through to the final machine code (e.g. virtual functions, RTTI) and what gets dumped during compilation (e.g. classes, attributes of data members, access specifiers).

[1] Strictly after linking - before then you might be able to work out a function''s const from it''s decorated name. If you do that they you have too much time on your hands!


如果取决于编译器,那根本就不是问题,只是因为在源代码中指出了所有编译所需的内容代码.我认为合理的编译器设计不应将常量函数与其他成员分开使用.保持恒定与否仅仅是类成员的众多属性之一.当然,编译器代码会将解析后的成员信息保留为某种数据结构;有什么特别之处?

我们无权访问此特定编译器Microsoft VC ++的源代码.如果您想了解这样的细节,请考虑查看例如GNU C ++的源代码:
http://en.wikipedia.org/wiki/GNU_Compiler_Collection [ http://gcc.gnu.org/ [ ^ ].

源代码可以在这里找到:
http://gcc.gnu.org/svn.html [
If depends on the compiler, and is not a problem at all, just because all needed for compilation is indicated in the source code. I don''t think a reasonable compiler design should tread a constant function separately from other members. Being constant or not is just one of the many attributes of a class member. Of course the compiler code keeps the parsed member information is some data structure; what''s so special about it?

We do not have access to the source code of this particular compiler, Microsoft VC++. If you want to know such detail, consider looking at, for example, the source code of GNU C++:
http://en.wikipedia.org/wiki/GNU_Compiler_Collection[^],
http://gcc.gnu.org/[^].

The source code can be found here:
http://gcc.gnu.org/svn.html[^].

Good luck,
—SA


这篇关于编译器如何了解const成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆