944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7639
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 5th, 2007
0

Private Access

Expand 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 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.
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by shouvik.d ...
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.
Click to Expand / Collapse  Quote originally posted by shouvik.d ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by Ravalon ...
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
CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by Bench ...
Actually, I don't think there's anything which stops it. Consider the following code
CPP Syntax (Toggle Plain Text)
  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'.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by Ravalon ...
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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by Bench ...
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.
Click to Expand / Collapse  Quote originally posted by Bench ...
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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 5th, 2007
0

Re: Private Access

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
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by shouvik.d ...
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().
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 5th, 2007
0

Re: Private Access

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
Reputation Points: 64
Solved Threads: 7
Junior Poster
shouvik.d is offline Offline
198 posts
since Jan 2007
Jan 5th, 2007
0

Re: Private Access

Click to Expand / Collapse  Quote originally posted by shouvik.d ...
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.
C++ Syntax (Toggle Plain Text)
  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().
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Adding Random Numbers help
Next Thread in C++ Forum Timeline: Need help using GetCursorPos()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC