Accessing base class friend private data members

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 2
Reputation: Phloxicon is an unknown quantity at this point 
Solved Threads: 0
Phloxicon Phloxicon is offline Offline
Newbie Poster

Accessing base class friend private data members

 
0
  #1
Sep 10th, 2009
This would be trivial but it comes with a serious catch - I can't edit the Unrelated or Friend class. I can't seem to access a private data member despite the friend relationship between the classes. Code:
http://pastie.org/613042
  1. class UnrelatedClass
  2. {
  3. ...
  4. private:
  5. friend class FriendClass;
  6. float m_fNumber;
  7. }
  8.  
  9. class FriendClass
  10. {
  11. public:
  12. CHandle<UnrelatedClass> m_hUnrelatedClass;
  13. void Bar();
  14. }
  15.  
  16. class DerivedClass : public FriendClass
  17. {
  18. public:
  19. ...
  20. void Foo();
  21. ...
  22. }
  23.  
  24. void FriendClass::Bar()
  25. {
  26. m_hUnrelatedClass->m_fNumber = 2.0; // no error
  27. }
  28.  
  29. void DerivedClass::Foo()
  30. {
  31. ...
  32. FriendClass *friendClass = static_cast<FriendClass *>( this );
  33. friendClass->m_hUnrelatedClass->m_fNumber = 2.0; // produces error
  34. ...
  35. }
  36.  
  37. // error C2248: 'UnrelatedClass::m_fNumber' : cannot access private member declared in class 'UnrelatedClass'

I don't understand how the compiler can tell the difference between being in the class and just have it accessed from an object. Is there anyway around this WITHOUT altering the FriendClass or UnrelatedClass? I very much appreciate your thoughts on this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 61
Reputation: AceofSpades19 is on a distinguished road 
Solved Threads: 10
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Junior Poster in Training

Re: Accessing base class friend private data members

 
1
  #2
Sep 10th, 2009
Classes don't inherit friends and subclasses can't access base class's private members
Last edited by AceofSpades19; Sep 10th, 2009 at 11:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: Phloxicon is an unknown quantity at this point 
Solved Threads: 0
Phloxicon Phloxicon is offline Offline
Newbie Poster

Re: Accessing base class friend private data members

 
0
  #3
Sep 11th, 2009
Originally Posted by AceofSpades19 View Post
Classes don't inherit friends and subclasses can't access base class's private members
I understand both of those facts but I thought that here, I'm only accessing the private members via the friend class, which I thought would be okay.

Is there any ugly hack I can use to get around this?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Accessing base class friend private data members

 
0
  #4
Sep 11th, 2009
I understand both of those facts but I thought that here, I'm only accessing the private members via the friend class, which I thought would be okay.
Making a friend to a class does not make the private members public. FriendClass can access UnrelatedClass' private members, but those private members are still private. Users of FriendClass cannot access them any more than non-friends of UnrelatedClass can.

Is there any ugly hack I can use to get around this?
There are a few, and none of them are recommended. But since you asked for an ugly hack, I will show you one. Use it at your own risk:
  1. void DerivedClass::Foo()
  2. {
  3. *reinterpret_cast<float*>(m_hUnrelatedClass) = 2.0;
  4. }
This hack assumes that CHandle<> is a smart pointer. Do whatever you need to do to get the address of the UnrelatedClass object pointed to by the field.

It works like this: UnrelatedClass has no virtual members, which means the object structure is easier to predict with some accuracy. There is only one field and it is the field you want, so you can assume that for an object of UnrelatedClass, &obj.fNumber is the same address as &obj. If that is true and you cast that address to float*, you can access fNumber even though it is private.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC