void fun(int a,int* b,int& c,int*& d)
{
a=0;
*b=2;
c=3;
*d=4;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=0;
int* b=&a;
int& c=a;
int*& d=b;
fun(a,b,c,d);
cout<<a<<" "<<*b<<" "<<c<<" "<<*d<<endl;
getchar();
return 0;
}
考察指针、引用、传值、传地址、传引用等概念,最后输出: 4 4 4 4
class BaseBase{
public:
BaseBase(){cout<<"BaseBase()"<<endl;}
BaseBase(const string& s){cout<<"BaseBase(const string&)"<<endl;}
~BaseBase(){cout<<"~BaseBase()"<<endl;}
};
class Base1:virtual public BaseBase{
public:
Base1(){cout<<"Base1()"<<endl;}
Base1(const string& s){cout<<"Base1(const string&)"<<endl;}
~Base1(){cout<<"~Base1()"<<endl;}
};
class Base2:virtual public BaseBase{
public:
Base2(){cout<<"Base2()"<<endl;}
Base2(const string& s){cout<<"Base2(const string&)"<<endl;}
~Base2(){cout<<"~Base2()"<<endl;}
};
class Base3{
public:
Base3(){cout<<"Base3()"<<endl;}
Base3(const string& s){cout<<"Base3(const string&)"<<endl;}
~Base3(){cout<<"~Base3()"<<endl;}
};
class Derived:public Base1,public Base2,public Base3{
public:
Derived(const string& s):Base1(s),Base2(s){cout<<"Derived(const string&)"<<endl;}
~Derived(){cout<<"~Derived()"<<endl;}
};
void main()
{
Derived d(const string("hello word"));
getchar();
}
输出:
BaseBase()
Base1(const string&)
Base2(const string&)
Base3()
Derived(const string&)
~Derived()
~Base3()
~Base2()
~Base1()
~BaseBase()
需要注意的是BaseBase(),在虚继承中,最后的派生类必须给出虚基类的构造方式,中间派生类的构造方式对虚基类不起作用,此处Derived没有指定BaseBase的构造方式,因而编译器会用默认的无参构造函数来构造BaseBase。
参考:2013年海康威视校园招聘笔试题 http://blog.csdn.net/hackbuteer1/article/details/8476206