+ 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?

8th Jul 2021, 1:48 AM
Rishi
Rishi - avatar
2 odpowiedzi
+ 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); }
8th Jul 2021, 3:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Hatsy Rei oh yes I got it. Thank you ^-^
8th Jul 2021, 11:11 AM
Rishi
Rishi - avatar