Archived

This topic is now archived and is closed to further replies.

Redoc

Class - private

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 .

Share this post


Link to post
Share on other sites
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 public


struct MyInternalData;

class MyClass
{
private:
MyInternalData data;
public:
Operation1();
Operation2();
Operation3();
};

//in another header file, which you don''t release


struct 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites