static成员没有this指针是关键!
static
function都是静态决议的(编译的时候就绑定了)
而virtual function 是动态决议的(运行时候才绑定)
例证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
#include <iostream> #include <bitset> using
namespace
std; class
A { public : A( int
a) { this ->val2 = a; } static
void
get_val() { this ->val2 = 4; cout << "hello"
<< val << endl; } private : static
int
val; int
val2; }; int
A::val = 4; int
main() { A a(2); a.get_val(); } |
编译错误
虚函数(virtual)为啥不能是static,布布扣,bubuko.com
原文:http://www.cnblogs.com/kaituorensheng/p/3616956.html