4 ms·
In C++ you can access a private member of a class A by defining a class B as exactly the same but making that member public, and casting to B*: B* b = (B*)&a; b
by zerr 4y ago
In C++ you can access a private member of a class A by defining a class B as exactly the same but making that member public, and casting to B*: B* b = (B*)&a; b->private_member... Shouldn't be used more than once in every 20 years though :)
- robalni 4y agoThis first seems like a pointless thing to do because you could just make it public in class A. But then when I think about it more I realize that it might actually be pretty useful if you have a huge codebase and it would take 30 minutes to recompile if you change class A that is probably defined in a header file that is included everywhere. This trick can then save 30 minutes.
- zerr 4y agoThis is useful when class A is from system or 3rd party library header so you can't just change it (unless you want to maintain the patch).