Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger event on return in C++

Tags:

c++

I want to execute some function right before the return of another function. The issue is that there are multiple returns and I don't want to copy-paste my call before each of them. Is there a more elegant way of doing this?

void f()
{
    //do something
    if ( blabla )
        return;
    //do something else
    return;
    //bla bla
}

I want to call g() before the function returns.

like image 595
Luchian Grigore Avatar asked Nov 28 '22 03:11

Luchian Grigore


1 Answers

struct DoSomethingOnReturn {
  ~DoSomethingOnReturn() {
    std::cout << "just before return" << std::endl;
  }
};
...
void func() {
  DoSomethingOnReturn a;
  if(1 > 2) return;      
}
like image 51
Andrey Agibalov Avatar answered Dec 10 '22 12:12

Andrey Agibalov