Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member functions same as passing reference to global function?

Can I be sure that:

class foo {
  public:
  int x;
  void bar(int k) {
    x = k;
  }
};
foo o;
o.bar(5);

Will be the same as:

class foo {
  public:
  int x;
};

void foobar(foo& f, int k) {
  f.x = k;
}

foo o;
foobar(o, 5);

I know both will set "x" to "k", but can I be sure that they both perform at same speed / generate same asm? Can the compiler optimize methods greater?

like image 853
Pubby Avatar asked May 10 '26 16:05

Pubby


1 Answers

Generate the assembler and compare (-S flag for GCC).

like image 109
Nikolai Fetissov Avatar answered May 12 '26 07:05

Nikolai Fetissov