forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_inline.cpp
44 lines (40 loc) · 1.01 KB
/
virtual_inline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @file virtual_inline.cpp
* @brief 通常类成员函数都会被编译器考虑是否进行内联。
* 但通过基类指针或者引用调用的虚函数必定不能被内联。
* 当然,实体对象调用虚函数或者静态调用时可以被内联,虚析构函数的静态调用也一定会被内联展开。
* @author 光城
* @version v1
* @date 2019-07-24
*/
#include <iostream>
using namespace std;
class Base
{
public:
virtual void who()
{
cout << "I am Base\n";
}
};
class Derived: public Base
{
public:
void who()
{
cout << "I am Derived\n";
}
};
int main()
{
// note here virtual function who() is called through
// object of the class (it will be resolved at compile
// time) so it can be inlined.
Base b;
b.who();
// Here virtual function is called through pointer,
// so it cannot be inlined
Base *ptr = new Derived();
ptr->who();
return 0;
}