![]() |
| | #1 |
| Registered User Join Date: Aug 2002
Posts: 4
| Newbie question about the Private type in classes .But, what is the point of the private access type in classes? Is that just there so YOU don't accidentally modify that, and the compiler gives you a kick upside the head if you do? What am I overlooking? Are they really that useless? |
| Ryeguy457 is offline | |
| | #2 |
| Guest Join Date: Aug 2001
Posts: 5,034
| class Object { private: int safest; protected: int safe; public: int exposed; }; class NewObject : public Object { public: void manipulate() { exposed = 12; //..fine... safe = 2; //...fine. Derived classes can access "protected:" safest = 6; //...cannot do! private to "Object" class... } }; int main() { Object ob; ob.safest = 10;//...no way... ob.safe = 12;//...nope ob.exposed = 13; //...no problem return 0; }; Ok, so you knew all that, right? Just a refresher. The idea behind it all is "data-hiding". You can use it as a tool to remind yourself that you didn't want to be able to access a member of an objects class directly, (maybe there are butterfly effect implications) or similar reasons. In other realms of programming, this mechanism has helped software library authors control what goes in and out of a class instance, information-wise, providing a sort of controlled-component functionality. But there are many other possible reasons. The point is, unless it's necessary for you at the moment, don't bother. But when you DO need it, you'll be glad it there |
| Sebastiani is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question with accessing private data | mikahell | C++ Programming | 3 | 01-18-2008 03:14 AM |
| Working with winAPI in classes (newbie question) | Spyril | Windows Programming | 3 | 11-09-2007 03:21 PM |
| Type conversion question. | ellis | C++ Programming | 5 | 10-17-2006 04:05 PM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| Using VC Toolkit 2003 | Noobwaker | Windows Programming | 8 | 03-13-2006 07:33 AM |