Private Access

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Private Access

 
0
  #1
Jan 5th, 2007
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.
  1. class x
  2. {
  3. private:
  4. void meth1()
  5. {
  6. cout<<"Meth1";
  7. }
  8. void meth2()
  9. {
  10. cout<<"meth 2";
  11. }
  12. };
  13. class xduplicate
  14. {
  15. public:
  16. void virtual meth1();
  17. void virtual meth2();
  18. };
  19. main()
  20. {
  21. x obj;
  22. xduplicate *obj1;
  23. obj1=(xduplicate *)&obj;
  24. obj1->meth1();
  25. }
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
  1. #define private public
but this unwantedly unveals many non required methods toopen arena

plz hlp
Last edited by shouvik.d; Jan 5th, 2007 at 3:58 am.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #2
Jan 5th, 2007
Originally Posted by shouvik.d View Post
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 only other safe way is a member function of a friend class. I can think of three unsafe ways to do it, but they're all hit or miss and they all break something serious. I think you might be misunderstanding the requirement for your project.
Originally Posted by shouvik.d View Post
though one of the cheap n unprofesssional sol is
  1. #define private public
but this unwantedly unveals many non required methods toopen arena
It's also not allowed. The language standard doesn't let you redefine keywords. It's also not sure to work if the private section isn't explicit. For example.
  1. class x
  2. {
  3. void meth1()
  4. {
  5. cout<<"Meth1";
  6. }
  7. void meth2()
  8. {
  9. cout<<"meth 2";
  10. }
  11. };
The default access level of a class is private, but your trick won't work with the above class because it doesn't use the private keyword explicitly. It also won't work if you don't have access to the source code because by the time a class is compiled to object code, the keywords are long gone and the preprocessor from client code won't be able to touch it.

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Private Access

 
0
  #3
Jan 5th, 2007
Originally Posted by Ravalon View Post
It's also not allowed. The language standard doesn't let you redefine keywords.
Actually, I don't think there's anything which stops it. Consider the following code
  1. #undef private
  2. #define private public
  3.  
  4. int main()
  5. {
  6. }
On at least 2 different compilers in strict mode, this compiles without error nor warnings.

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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #4
Jan 5th, 2007
Originally Posted by Bench View Post
Actually, I don't think there's anything which stops it. Consider the following code
  1. #undef private
  2. #define private public
  3.  
  4. int main()
  5. {
  6. }
On at least 2 different compilers in strict mode, this compiles without error nor warnings.
The compilers might let it slide, but the language standard says it's undefined behaviour. That's legalese for 'not allowed'.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Private Access

 
0
  #5
Jan 5th, 2007
Originally Posted by Ravalon View Post
The compilers might let it slide, but the language standard says it's undefined behaviour. That's legalese for 'not allowed'.
In which case I stand corrected 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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #6
Jan 5th, 2007
Originally Posted by Bench View Post
I haven't got access to a copy of the standard right now
Me neither. I'm just going by memory, and I could very well be wrong.
Originally Posted by Bench View Post
although i'm somewhat surprised that Comeau allowed that through without even throwing a warning.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #7
Jan 5th, 2007
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
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #8
Jan 5th, 2007
Originally Posted by shouvik.d View Post
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
That looks like a different problem, you're using console mode code in a Win32 mode project. It expects WinMain() as the entry point instead of main().
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #9
Jan 5th, 2007
it isn't a console based application.

Simple win32 appl. and main() itself is working as an entry point if i remove the lines
  1. #undef private
  2. #define private public
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Private Access

 
0
  #10
Jan 5th, 2007
Originally Posted by shouvik.d View Post
it isn't a console based application.

Simple win32 appl. and main() itself is working as an entry point if i remove the lines
  1. #undef private
  2. #define private public
Right. You're using main() as the entry point, like this.
  1. int main()
  2. {
  3. return 0;
  4. }
But because it's a Win32 application, the runtime code is looking for WinMain() and not main().
  1. #include <windows.h>
  2.  
  3. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  4. {
  5. return 0;
  6. }
The linker throws that error to tell you that it can't find WinMain.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC