A non-virtual method is just like a normal global function in this regard except that it accepts an extra hidden this pointer as its first parameter. The address of a non-virtual method is a fix location in your process address space.
This may be a stupid example but the following code:
struct S
{
int i;
void f()
{
printf("%d\n", i);
}
}
S s;
s.f();
...is basically compiled to something like this:
struct S
{
int i;
}
void f(S* _this_)
{
printf("%d\n", _this_->i);
}
S s;
f(&s);
Knowing this reveals that there is no magic happening when you issue an
s.f()
method call. You just call the
f()
"function" residing in the
S
namespace by specifying an
S
instance as its first hidden parameter (that is
&s
in our case).
The only difference is that the readability of the code in the first block tends to be better (in my opinion - especially when the size of codebase increases) and there you can hide method
f()
from outer world by making it private and
f()
can see the private members of S (this is some kind of documentation and prevention of abuse - converting human coder mistakes into compile errors), the same isn't true for the second code block that is much like C code. I usually start to explain the beauty of C++ for C coders through a similar example on non-virtual methods. :-)