Solved

C++ Accidental Access to Private Object Variable

Posted on 2013-01-11
4
614 Views
Last Modified: 2013-01-11
Given a class with a private variable and a public getter method, if a programmer were to return a reference to the private variable, then users of that method could have direct modifiable access to that private variable (i.e., modify the variable value without using an object method).

I don't like this. (Tough luck on me, I guess. But why would the C++ standard allow this?)

Is there a way to keep private variables truly private so that the above cannot happen?
0
Comment
Question by:phoffric
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
Comment Utility
>> why would the C++ standard allow this?
Simple answer: because it does - it is up to you as the coder to ensure your code is safe. C++, unlike most other languages, doesn't hold your hand.

>> Is there a way to keep private variables truly private so that the above cannot happen?
Yes, return a const reference/pointer... although there is nothing to prevent the user from casting const away.
0
 
LVL 32

Author Closing Comment

by:phoffric
Comment Utility
Darn, I am being considered for a lead position at a new company, so I started think about how someone can mess things up and thought about these kinds of problems. I knew about your suggestions, but I guess it will be the code reviews that will have to save us from this type of coding from getting into production. Without the code reviews to catch this, no doubt initial systems testing will pass fine. But someday, someone would start modifying these private object variables using their own "local" variables.

I heard that C can shoot you in the foot; and that C++ tries to improve on that; but when the coding goes too far awry, C++ can blow off your face.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
C++, like C, is a very low-level programming language. It gives you the tools but doesn't really stop you abusing them. Just like a hammer, when used incorrectly, can cause havoc so can C/C++. If you are the team lead you should ensure you have a coding standard document and ensure all your team adhere to it.  If you don't have one then this is a good place to start.
0
 
LVL 32

Author Comment

by:phoffric
Comment Utility
Thanks! I have worked with government coding standards; yet I have not seen the protection required to avoid the above potential maintance issue. There were some useful things like:
 if ( x == 0 ) {...} // not allowed
 if ( 0 == x ) {...} // allowed
to prevent accidental use of = instead of ==.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now