| | |
Accessing Private data memebrs outside the class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class emp { private : int i ; int j; public : emp( ) { i = 10 ; j = 50 ; } void display() { cout<<endl<<i<<" "<<j<<endl; } } ; int main( ) { emp *p = new emp ; p->display(); int *pi = (int*) p ; cout << *pi ; *pi = 20 ; p->display(); delete p; return 0; }
First: Isn't the above code shows flaw/hole in the language??I am able to access the private member of the class through typecasting
Second:If you see the output of this code...it comes out to be
10 50
10
20 50
How can i change the private data member j through the typecasted pointer
>Isn't the above code shows flaw/hole in the language??
No. If C++ were designed to avoid both stupid breaking of the rules and willful/malicious breaking of the rules, the language would be a beast that nobody wanted to use. C++ protects you from your stupidity, but if you know how to sidestep the protection, that's your business.
>How can i change the private data member j through the typecasted pointer
That's one way you can, since you asked. But you shouldn't. Private members are private for a reason, and you should respect that. Not to mention that if you have to ask about this, you're probably not aware of the many (many!) pitfalls and potential undefined behavior of what you want to do.
No. If C++ were designed to avoid both stupid breaking of the rules and willful/malicious breaking of the rules, the language would be a beast that nobody wanted to use. C++ protects you from your stupidity, but if you know how to sidestep the protection, that's your business.
>How can i change the private data member j through the typecasted pointer
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class emp { int i; int j; public: emp(): i ( 10 ), j ( 20 ) {} void display() { cout<< i <<' '<< j <<'\n'; } }; int main() { emp e; unsigned char *p = reinterpret_cast<unsigned char*> ( &e ); int *pi = reinterpret_cast<int*> ( p ); int *pj = reinterpret_cast<int*> ( p + sizeof ( int ) ); e.display(); ++*pi; --*pj; e.display(); return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- only const static integral data members can be initialized inside a class or struct (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: plzzzzzzzzzzzzzzzzzz help
- Next Thread: C++ Time Conversion. HEEEEELP plz
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






