944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1533
  • C++ RSS
Oct 31st, 2006
0

C++ conceptual question

Expand Post »
Hello all,

While I understand what the class access keywords public, private, and protected are supposed to do, I just don't see how this ACTUALLY "hides variables" and makes for more bullet proof code.

Do the variable names survive the compilation process?
If someone was to read compiled code in raw memory, would they see the variable names?

Also, the process of taking one variable name in a public funtion, and then assigning it's value to a private variable seems redundant and a bit convoluted to me.

Also the concept of 'constant functions" seems contrived. What is really happening with that?
A function can have a constant and non constant version just by virtue of class definition?

If some program went awry, wouldn't that 'constant" piece of code get "stepped on"? After all, both are just memory locations! I don't fully appeciate the difference.

Perhaps I'm just "thinking into it" too much, but I do like to understand what I'm doing and WHY.

Thanks in advance for any light you can shed on this.
Similar Threads
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Oct 31st, 2006
0

Re: C++ conceptual question

>>Do the variable names survive the compilation process? If someone was to read compiled code in raw memory, would they see the variable names?
No. Compiled porgrams know nothing about variable names -- they are all nothing more than addresses. Variable names are only for you, the human programmer.


>>Also, the process of taking one variable name in a public funtion, and then assigning it's value to a private variable seems redundant and a bit convoluted to me

This has the advantage of allowing only one place in the program to change the value of a class variable. If you later want to change the way the variable is used the programmer has to make the change in only one place.

>>Also the concept of 'constant functions" seems contrived. What is really happening with that?

const tells you and the compiler that the object can not be changed or that the const function is not going to change any data objects of the class. It has no affect on the code itself, but is a retraint on you, the programmer, not to mess around with class objects -- class objects are read-only. Also, a const class method can only call other const methods of the same class.

>>If some program went awry, wouldn't that 'constant" piece of code get "stepped on"? After all, both are just memory locations! I don't fully appeciate the difference

Const is a compile-time restriction. It has no affect at run-time; the compiled program knows nothing about the const keyword. All const problems must be resolved at compile time, so at runtime there will never be any such problems.
Last edited by Ancient Dragon; Oct 31st, 2006 at 11:39 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ 2005 question
Next Thread in C++ Forum Timeline: Overloading the increment operator plz help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC