Thread: Question with accessing private data

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Question with accessing private data

    Hi! I started learning to build DLLs, and I discovered something that I find weird. Probably other people have realised this already, or maybe my compiler is just doing funny things... Well, here's what I came across. I built a little dll for learning purposes and inside of it, I exported a class, so that I could test it in another app where I'd import the test dll with its header files. So in the test app, I saw that I could remove the "private:" tags and replace them with "public:" giving me direct access to everything in the class... Then my app would compile fine even if I tried to access private data that should be hidden in the dll... That means that if you give the dll to someone, he'll be able to mess with it? Shouldn't functions and variables names decoration also decorate the public/private tag? Maybe I am getting confused with all data protection thing...

    Would that also mean that users could add new functions to your classes, having tweaked your header files and building their new functions definitions too?


    Code:
    //Class's header as compiled in the DLL
    class foo
    {
    private:
        int data;
    };
    
    //You gave your headers to someone then he messed with it
    class foo
    {
    public:
        int data;
    };

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Shouldn't functions and variables names decoration also decorate the public/private tag?
    That's an interesting concept, but I don't think it's required by the standard.

    >> giving me direct access to everything in the class...
    The standard may say or imply that this would result in "implementation defined" behavior - I'm not sure. In reality, you can usually "get away" with it since changing the access type doesn't change the binary representation of the class object in memory. It basically provides syntactic access to class members.

    >> and building their new functions definitions too?
    It's possible. You could probably add inline methods to the class that simply access existing members. You might not be able to add new virtual methods, specially if the object comes from a class factory you don't have the source code to.

    The PIMPL idiom is good for when you really want to hide the implementation details of your class.

    gg

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Okay, so I see it has to do with standards... I didn't know about the PIMPL though, maybe I can get something out of this! Thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that if you REALLY want to get the data out of a class, you can - no matter what the private/public tags say. Just take the address of the class, and cast it to some sort of generic pointer [e.g. a pointer to unsigned int].

    public/protected/private are there to show programmers that they are doing someething wrong, not prevent the data from being accessed.

    As Codeplug explains, you can "hide" the implementation, but again, it's only good for HIDING the content of the class. Someone sufficiently inclined to change the content of the class will still be able to - you just put another small obstacle in the way. If the code can see the object, then it can access any content - at least using currently available memory models for available major OS's.

    Also, changing the private: to public: may change the layout of the data in the class, so it's not guaranteed that this will actually achieve what the "client" wants - it may just break the DLL's compatibility with the application...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Replies: 26
    Last Post: 06-15-2005, 02:38 PM