+ 1
C++ class member functions
We access the member function of a class by using an object and the dot operator. We know that the compiler implicitly modifies it to the "this" pointer syntax. For example, "obj.add ()" becomes "add(&obj)" implicitly My doubt is, is there any way to explicitly use this kind of syntax and get successful result. Can we mention it as "add(&obj)" and will the compiler won't implicitly change it and execute it?
2 ответов
+ 5
Probably not possible, at least not without explicitly declaring an overload of that method which takes a pointer to the class, and even if you do so there's a chance the compiler does something else under the hood (which is not part of the C++ standard, but instead platform dependent). As such you are not really calling the hidden method which only the compiler has access to, just simulating it.
#include <iostream>
class A {
public:
int func() {
return 1;
}
static int func(A* obj) {
return obj->func();
}
};
int main() {
A obj;
std::cout << A::func(&obj);
}
+ 1
Hatsy Rei oh yes I got it. Thank you ^-^