Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ Accidental Access to Private Object Variable

Posted on 2013-01-11
4
Medium Priority
?
647 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 800 total points
ID: 38769270
>> 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
ID: 38769279
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
ID: 38769290
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
ID: 38769304
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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