Class - private

Started by
5 comments, last by Redoc 20 years, 6 months ago
Hello, Im trying to learn about classes but there is something thats bothering me. The point in creating a class is to wrap code up? And then you are able to pass it on to someone else who uses the class and calls its functions. To stop other people from accessing the mechanics which could cause problems you place your varibles in the "private" declaration, and only give access to function you want them to have in the "public" declaration. So you make a class and pass it on.. whats to stop them from editing your code and moving variables around etc? Would you need to submit a form of compiled class? I hope thats clear, its my understanding of how its working. Thanks for any light.
Advertisement
The purpose of private variables is not to protect your classes from being intentionally messed with. There are plenty of ways to do that. The purpose is to prevent people from unintentionally messing with them.

How appropriate. You fight like a cow.
What Sneftel said. It also forces you into a more OOP-like style of programming, something that is not always natural to do. It really comes in handy later though when you can take one junked up class, replace it with a fixed up one, and have it compile right the first time .
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
Yup, it''s not really there to force people to play it your way - just to stop them being lazy, to call getter/setter methods instead of accessing the variable directly, etc.

If you *really* wanted to protect some of your class, you could use something like this:

// in the header file you release to the publicstruct MyInternalData;class MyClass{ private: MyInternalData data; public: Operation1(); Operation2(); Operation3();};//in another header file, which you don''t releasestruct MyInternalData{ int dataItem1; int dataItem2;};


If you compile your code as a library with both headers, and then distribute the compiled .LIB and the first header, then I think it works. People can still mess with the contents of the MyInteralData structure by trying to directly write to the object''s memory, but nobody realistically does that.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

superpig: It must be a pointer to an object (and not an object) for that to work, or else the compiler will not know how big the class will be.

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
quote:The point in creating a class is to wrap code up? And then you are able to pass it on to someone else who uses the class and calls its functions.


Classes model behavior that is exposed through a public interface. If you just want to model data with no behavior, use a struct.


--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
quote:Original post by Redoc
...whats to stop them from editing your code and moving variables around etc?
Nothing, if you give them the code.

quote:Would you need to submit a form of compiled class?
Yep. Generally, you give them header files (stripped of all private members and superfluous comments) and compiled static or dynamic library files that they can link to. If they alter the header files in non-intrusive ways (adding more whitespace doesn''t change the semantic interpretation of the file; adding a new data member does) then they are unable to link against your supplied libraries. Ergo, protection.

This topic is closed to new replies.

Advertisement