Data Encapsulation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neo

    #1

    Data Encapsulation?

    I was written this code; in VS2k5



    class Program

    {

    private: int a;

    private: int b;

    private: char c;

    public: Program( ) : a(10), b(20), c('Y'){ }

    };



    int _tmain(int argc, _TCHAR* argv[])

    {



    A obj;

    int temp = *((int *)&obj + 1);

    char ch = *((char *)&obj + 8);

    cout<< temp<<endl<<ch< <endl;

    _gettch( );

    return 0;

    }



    in Object Orient World we learn Data Encapsulation or Data Hiding for data
    security.

    In this code, what is the meaning of Data Encapsulation or Data Hiding for
    data security?



    Regards,

    -neo


  • Vinzenz Feenstra

    #2
    Re: Data Encapsulation?

    Neo schrieb:
    in Object Orient World we learn Data Encapsulation or Data Hiding for data
    security.
    >
    In this code, what is the meaning of Data Encapsulation or Data Hiding for
    data security?
    In this code the class members are hidden and not visible to non-friends.

    If you would try to access the members the correct way:

    e.g. obj.a = 10;

    you will get a compiler error. This would be the correct behaviour, but
    you are using a bad hack. Of course your code *can* work, but it has not
    to work. If you have vtable this can result in undefined behaviour.

    There is no security of accessing if you access elements in this way.
    And there can't be such a security.

    Regards,
    Vinzenz

    Comment

    • Arnaud Debaene

      #3
      Re: Data Encapsulation?


      "Neo" <momer114@hotma il.coma écrit dans le message de news:
      %23VG7crxqGHA.3 484@TK2MSFTNGP0 4.phx.gbl...
      >I was written this code; in VS2k5
      <snip>>
      A obj;
      >
      int temp = *((int *)&obj + 1);
      char ch = *((char *)&obj + 8);
      cout<< temp<<endl<<ch< <endl;
      >
      in Object Orient World we learn Data Encapsulation or Data Hiding for data
      security.
      >
      In this code, what is the meaning of Data Encapsulation or Data Hiding for
      data security?
      The aim of data access restriction (protected, private) in OO language is to
      protect against Murphy - that is the programmer inadvertently
      reading/writing a variable it shouldn't be interested in.
      It is not aimed at protecting against Machiavel - that is a hacker who want
      to acess data that should be forbidden to it. If you nee that kind of
      protection, you probably need cryptography, authentificatio n, access
      control, etc...
      Anyway, as Vinzenz has said, your "solution" is a hack that depends on
      knowledge of the compiler internals working (ie, data padding and
      alignement, presence of a v-ptr, data member reorganization, etc...)

      Note that this is true in all OO languages, it is not specific to C++.

      Arnaud
      MVP - VC


      Comment

      Working...