Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC
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 5-Jun-12 8:35am
naaryan723
Comments
SAKryukov at 5-Jun-12 15:16pm
   
Why would you need to learn how? Wants to write your own compiler?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  
Comments
naaryan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
CPallini at 5-Jun-12 16:26pm
   
My 5 for your 'valuable suggestion'.
losmac at 5-Jun-12 16:27pm
   
I agree with you. My 5!
SAKryukov at 5-Jun-12 16:34pm
   
Thank you, Carlo, Maciej. :-)
--SA
naaryan at 6-Jun-12 14:24pm
   
Thanks for your valuable solution.
SAKryukov at 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)

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 200
2 Rob Philpott 100
3 jlopez788 99
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 6 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100