Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 5-Jun-12 15:16pm    
Why would you need to learn how? Wants to write your own compiler?
--SA

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!
 
Share this answer
 
Comments
naaryan 6-Jun-12 14:23pm    
Thanks for your kind suggestion.

If you provide some good reference/link for address space model of multi-threaded program. basically how it share memory of the process who create the thread.
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
 
Share this answer
 
v2
Comments
CPallini 5-Jun-12 16:26pm    
My 5 for your 'valuable suggestion'.
Maciej Los 5-Jun-12 16:27pm    
I agree with you. My 5!
Sergey Alexandrovich Kryukov 5-Jun-12 16:34pm    
Thank you, Carlo, Maciej. :-)
--SA
naaryan 6-Jun-12 14:24pm    
Thanks for your valuable solution.
Sergey Alexandrovich Kryukov 6-Jun-12 14:48pm    
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900