C++的语法过于精巧,以致很多同学不能很好地理解。本文举几个小例子,供读者思考。
1、 inline和virtual
#include <iostream>
class A
{
public:
virtual inline void func()
{
std::cout << "A::func" << std::endl;
}
};
class B:public A
{
public:
virtual inline void func()
{
std::cout << "B::func" << std::endl;
}
};
int main(int argc,char *argv[])
{
A().func();
B().func();
A *p=new B();
p->func();
delete p;
}
2、 全局内置变量、静态内置变量、局部内置变量、寄存器内置变量、动态分配的内置变量的初始化问题
#include <iostream>
int a;
int &b(){static int result;return result;}
int main(int argc,char *argv[])
{
int c;
register int d;
int &e=*new int;
std::cout << a << std::endl;
std::cout << b() << std::endl;
//std::cout << c << std::endl;
//std::cout << d << std::endl;
std::cout << e << std::endl;
delete &e;
}