Click here to Skip to main content
11,920,377 members (46,323 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ VisualC++
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.

Posted 5-Jun-12 9:35am
SAKryukov 5-Jun-12 15:16pm
Why would you need to learn how? Wants to write your own compiler?
Rate this: bad
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!
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.
Rate this: bad
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++:[^],[^].

The source code can be found here:[^].

Good luck,
CPallini 5-Jun-12 16:26pm
My 5 for your 'valuable suggestion'.
losmac 5-Jun-12 16:27pm
I agree with you. My 5!
SAKryukov 5-Jun-12 16:34pm
Thank you, Carlo, Maciej. :-)
naaryan 6-Jun-12 14:24pm
Thanks for your valuable solution.
SAKryukov 6-Jun-12 14:48pm
You are very welcome.
Good luck, call again.

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

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
3 F-ES Sitecore 130
4 RyanDev 120
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 6 Jun 2012
Copyright © CodeProject, 1999-2015
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