943,703 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1680
  • C++ RSS
Sep 10th, 2009
0

Accessing base class friend private data members

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Phloxicon is offline Offline
7 posts
since Sep 2009
Sep 10th, 2009
1

Re: Accessing base class friend private data members

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.
Reputation Points: 61
Solved Threads: 10
Junior Poster in Training
AceofSpades19 is offline Offline
61 posts
since Jun 2008
Sep 11th, 2009
0

Re: Accessing base class friend private data members

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Phloxicon is offline Offline
7 posts
since Sep 2009
Sep 11th, 2009
0

Re: Accessing base class friend private data members

Quote ...
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.

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: openCV, TextBox and maybe more
Next Thread in C++ Forum Timeline: Borland, VS or something else?





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


Follow us on Twitter


© 2011 DaniWeb® LLC