2008年7月24日星期四

How complier invoke a member function

There are three steps complier to find function you call
1. Search function name match
2. Get the most approach one
3. Check right of function

class C
{
public:
void F(){}
};

class D : public C
{
void F(){}
};

...
D d;
d.F(); //wrong! why?

First, the complier find a function named F(). (D::F())
Second, check the param list
Third, check the right

Scenario 2:
class C
{
public:
void F(){}
};

class D : public C
{
int f;
};

...
D d;
d.F(); //wrong! why?
First, the compiler find name as f (D::f)
so why should we use m_f as member var name.

没有评论: