| | |
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:
Solved Threads: 0
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
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.
http://pastie.org/613042
c Syntax (Toggle Plain Text)
class UnrelatedClass { ... private: friend class FriendClass; float m_fNumber; } class FriendClass { public: CHandle<UnrelatedClass> m_hUnrelatedClass; void Bar(); } class DerivedClass : public FriendClass { public: ... void Foo(); ... } void FriendClass::Bar() { m_hUnrelatedClass->m_fNumber = 2.0; // no error } void DerivedClass::Foo() { ... FriendClass *friendClass = static_cast<FriendClass *>( this ); friendClass->m_hUnrelatedClass->m_fNumber = 2.0; // produces error ... } // 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.
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Classes don't inherit friends and subclasses can't access base class's private members
Is there any ugly hack I can use to get around this?
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
void DerivedClass::Foo() { *reinterpret_cast<float*>(m_hUnrelatedClass) = 2.0; }
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
![]() |
Similar Threads
- Why the member functions of one class can access to private data member of other obj? (C++)
- a class containg a set of data members (Python)
- C++ question class, friend operators (C++)
- Clearification with information hiding (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: openCV, TextBox and maybe more
- Next Thread: Borland, VS or something else?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list 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 text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





