•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,629 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1842 | Replies: 1
![]() |
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
#include <stdio.h>
#include <stdlib.h>
class A
{
int a;
public:
A()
{
a=100;
}
int get()
{
return a;
}
};
int main()
{
A *a=new A();
int *p=(int *)a;
printf("%d\n",a->get());
*p=200;
printf("%d\n",a->get());
system("pause");
}
its out put is -
100
200
so can any body help me to avoid this situation bcoz as we can observ the class definition we can access the private members.
#include <stdlib.h>
class A
{
int a;
public:
A()
{
a=100;
}
int get()
{
return a;
}
};
int main()
{
A *a=new A();
int *p=(int *)a;
printf("%d\n",a->get());
*p=200;
printf("%d\n",a->get());
system("pause");
}
its out put is -
100
200
so can any body help me to avoid this situation bcoz as we can observ the class definition we can access the private members.
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 10
What are you trying to avoid? Hacking? You hacked this and claim that you can access the private member a->a; All the 'private' designation means is that you can't access it via referencing it normally in the compiler; there's no implied protection of the memory area.
Try "a->a = 200;" and the compiler will tell you that you can't access a private member. That is a COMPILE time protection; what you demonstrated is a RUNTIME access.
Incidentally, add this to your definition of the class A:
virtual ~A() { a = 0; }
and try your hack. The output will read 100 and 100 and then your program will crash, because the vtable pointer for the destructor was set to 200.
Try "a->a = 200;" and the compiler will tell you that you can't access a private member. That is a COMPILE time protection; what you demonstrated is a RUNTIME access.
Incidentally, add this to your definition of the class A:
virtual ~A() { a = 0; }
and try your hack. The output will read 100 and 100 and then your program will crash, because the vtable pointer for the destructor was set to 200.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- AspectJ problem (Java)
- help with networking 9 computers (Networking Hardware Configuration)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
- Applications won't install at boot or, go polish your nails and wait (Windows NT / 2000 / XP / 2003)
- Suggestions on what type of programming language would be best for the situation (Computer Science and Software Design)
- Can't Setup IE 6 SP1 (Web Browsers)
Other Threads in the C Forum
- Previous Thread: What will be the O/P & why ?
- Next Thread: animation


Linear Mode