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
// 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.
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:
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.
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.