Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2006

Tip: Looking for answers? Try searching our database.

Data Encapsulation?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neo - 19 Jul 2006 11:09 GMT
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 - 19 Jul 2006 11:25 GMT
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
Arnaud Debaene - 20 Jul 2006 07:13 GMT
>I was written this code; in VS2k5
<snip>>
>            A obj;
>
[quoted text clipped - 7 lines]
> 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, authentification, 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.