Tagged bad practices
Accessing private class members in C++
Posted on by Idorobots
I've been called Kajtek "MOTHERFU*KINGWALLOFTEXT" Rzepecki lately, so let's make this post short.
We've got this code:
#include <iostream>
class A {
private:
int bar;
void foo() {
std::cout << "In A::foo() " << bar << "\n";
}
public:
//...
};
int main() {
A* a = new A();
//...
delete a;
return 0;
}
And we feel a sudden urge to call foo()
or access bar
. How do we do that?