Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between func() and (*this).func() in C++

Tags:

c++

dispatch

I am working on someone else code in C++, and I found a weird call to a certain function func(). Here is an example:

if(condition)     func(); else     (*this).func(); 

What is the difference between func() and (*this).func()?

What are the cases where the call to func() and (*this).func() will execute different code?

In my case, func() is not a macro. It is a virtual function in the base class, with an implementation in both base and derived class, and no free func(). The if is located in a method in the base class.

like image 325
gtatr Avatar asked Aug 04 '16 10:08

gtatr


People also ask

What is the difference between method and function in C?

Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.

What is the difference between pass by value and pass by reference?

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.

What are function pointers and the differences between normal function and function pointers?

1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.


2 Answers

There actually is a difference, but in a very non-trivial context. Consider this code:

void func ( ) {     std::cout << "Free function" << std::endl; }  template <typename Derived> struct test : Derived {     void f ( )     {         func(); // 1         this->func(); // 2     } };  struct derived {     void func ( )     {         std::cout << "Method" << std::endl;     } };  test<derived> t; 

Now, if we call t.f(), the first line of test::f will invoke the free function func, while the second line will call derived::func.

like image 56
lisyarus Avatar answered Oct 04 '22 13:10

lisyarus


It is impossible to tell from the snippet but possibly there are two callable objects called func(). The (*this).func(); makes sure the member function is called.

A callable object could be (for example) a functor or a lambda expression:

functor

struct func_type {     void operator()() const { /* do stuff */ } };  func_type func; // called using func(); 

lambda

auto func = [](){ /* do stuff */ }; // called using func(); 

For example:

#include <iostream>  class A { public:      // member      void func() { std::cout << "member function" << '\n'; }      void other()     {         // lambda         auto func = [](){ std::cout << "lambda function" << '\n'; };          func(); // calls lambda          (*this).func(); // calls member     } };  int main() {     A a;     a.other(); } 

Output:

lambda function member function 
like image 23
Galik Avatar answered Oct 04 '22 11:10

Galik