c++有太多的trick,下面是一个实例,代码如下:
class Father
{
public:
Father()
:count(0) {cout << "father create " << ++count << " times!" << endl;}
~Father() {cout << "father destory!" << endl;}
protected:
int count;
};
class Son : public Father
{
public:
Son(int i)
: Father(), ival(i)
{cout << "son create " << count << " times" << endl;
cout << ival << endl;}
~Son()
{
cout << "son destory!" << endl;
}
private:
const int& ival;
};
int main(int argc, char* argv[])
{
const int iv = 66;
Son son(iv);
son.S