| | |
Private Access
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Say if we ve certain private methods along with private data members and public methods.
other than friend function is thr any other way we access these members(they being private only) directly. its one of the requirements in a project of mine.
The real prob comes when accessing function.
say we've classes ported to us in .obj format. then going for DMA using pointers is a risky n unreliable job.
we can access meth1 only if in class x it is virtual. i.e. the entry is in the virtual table.
But now i cant jst change the native code ported to us. i've to anyhow access the private methods without any change in native code.
wht we're plannin is x will be the producer class n xduplicate the consumer class.
x will be available as .obj along with .h file
so how can 1.
though one of the cheap n unprofesssional sol is
but this unwantedly unveals many non required methods toopen arena
plz hlp
other than friend function is thr any other way we access these members(they being private only) directly. its one of the requirements in a project of mine.
The real prob comes when accessing function.
say we've classes ported to us in .obj format. then going for DMA using pointers is a risky n unreliable job.
c Syntax (Toggle Plain Text)
class x { private: void meth1() { cout<<"Meth1"; } void meth2() { cout<<"meth 2"; } }; class xduplicate { public: void virtual meth1(); void virtual meth2(); }; main() { x obj; xduplicate *obj1; obj1=(xduplicate *)&obj; obj1->meth1(); }
But now i cant jst change the native code ported to us. i've to anyhow access the private methods without any change in native code.
wht we're plannin is x will be the producer class n xduplicate the consumer class.
x will be available as .obj along with .h file
so how can 1.
though one of the cheap n unprofesssional sol is
c Syntax (Toggle Plain Text)
#define private public
plz hlp
Last edited by shouvik.d; Jan 5th, 2007 at 3:58 am.
Regards
Shouvik
Shouvik
•
•
•
•
Say if we ve certain private methods along with private data members and public methods.
other than friend function is thr any other way we access these members(they being private only) directly. its one of the requirements in a project of mine.
•
•
•
•
though one of the cheap n unprofesssional sol is
but this unwantedly unveals many non required methods toopen arenac Syntax (Toggle Plain Text)
#define private public
C++ Syntax (Toggle Plain Text)
class x { void meth1() { cout<<"Meth1"; } void meth2() { cout<<"meth 2"; } };
The simple answer is that you can't do what you're asking. The complicated answer is that you can do anything if you have sufficient knowledge of how the internals of the compiler work, but it's not portable and it's more likely to seriously screw something up than do any good.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
It's also not allowed. The language standard doesn't let you redefine keywords.
CPP Syntax (Toggle Plain Text)
#undef private #define private public int main() { }
Although i still think its a very bad idea. And, as you correctly mention, this doesn't change the fact that a class defaults all its members to private.
for the OP - Re-read the requirements of your problem. It sounds to me as if you've probably misinterpreted it.
Last edited by Bench; Jan 5th, 2007 at 12:00 pm.
¿umop apisdn upside down? •
•
•
•
Actually, I don't think there's anything which stops it. Consider the following codeOn at least 2 different compilers in strict mode, this compiles without error nor warnings.CPP Syntax (Toggle Plain Text)
#undef private #define private public int main() { }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
The compilers might let it slide, but the language standard says it's undefined behaviour. That's legalese for 'not allowed'.
I haven't got access to a copy of the standard right now - although i'm somewhat surprised that Comeau allowed that through without even throwing a warning. ¿umop apisdn upside down? Me neither. I'm just going by memory, and I could very well be wrong. 
If I remember correctly, redefining a keyword is undefined if a header is included but nothing is said about when a header isn't included. So your snippet might be well defined but pretty much any practical use is not. That might be hard to check for and the reason why Comeau was silent. It doesn't throw a warning if you include a header either.

If I remember correctly, redefining a keyword is undefined if a header is included but nothing is said about when a header isn't included. So your snippet might be well defined but pretty much any practical use is not. That might be hard to check for and the reason why Comeau was silent. It doesn't throw a warning if you include a header either.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
I couldn't make private definition to public
as it threw the followind error in VC++ 6.0
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/pvt.exe : fatal error LNK1120: 1 unresolved externals
as it threw the followind error in VC++ 6.0
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/pvt.exe : fatal error LNK1120: 1 unresolved externals
Regards
Shouvik
Shouvik
•
•
•
•
I couldn't make private definition to public
as it threw the followind error in VC++ 6.0
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/pvt.exe : fatal error LNK1120: 1 unresolved externals
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
it isn't a console based application.
Simple win32 appl. and main() itself is working as an entry point if i remove the lines
Simple win32 appl. and main() itself is working as an entry point if i remove the lines
c Syntax (Toggle Plain Text)
#undef private #define private public
Regards
Shouvik
Shouvik
•
•
•
•
it isn't a console based application.
Simple win32 appl. and main() itself is working as an entry point if i remove the lines
c Syntax (Toggle Plain Text)
#undef private #define private public
C++ Syntax (Toggle Plain Text)
int main() { return 0; }
C++ Syntax (Toggle Plain Text)
#include <windows.h> int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { return 0; }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Adding Random Numbers help
- Next Thread: Need help using GetCursorPos()
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





