the question is how to get pointer to self inside method of a class without touching this:
class Foo
{
int a, b, c;
void Print();
};
This way in common compiler I can do this refering to first data field:
void Foo::Print()
{
cout << &a; // == this
}
But are there any ways to do this without data members when only function exists?
class Foo2
{
void Print();
};
p.s. don’t even ask me why do I need this 🙂
For a POD class with at least one data member, the address of the class-type object is the same as the address of its first data member. This is because there can be no unnamed padding bytes before the first data member of a POD struct type. [In C++11, the rules are a bit different; I believe that this is true for all standard layout class types. I am not entirely familiar with the rules, however.]
For any other class type, there is no way to do this.