I was seeing some code and definition some where, like
class A {
public:
int a,b;
};
main() {
A a;
std::cout<<"Test output "<<&A::a<<" "<<&A::b<<std::endl;
}
Output
1 1
What I dont understand is a and b are not static members of A, but when they are accessed like a static member it gives an error, but when accessing the address of it like a static member prints 1. Is there a special meaning behind it, coz I have no idea why it is needed and why it works this way. Thanks.
The expression
&A::awhenais a non-static member returns a pointer-to-member-object (or pointer-to-member-function ifawere a function). One can use them like this:The stream output operation is not defined for pointer-to-member values, but there is a conversion from them to
boolwhich is what’s getting printed in your output.