海康威视2014校园招聘C++笔试题

这篇博客主要介绍了2014年海康威视校园招聘C++笔试中的一道题目,涉及指针、引用、值传递和引用传递等核心概念。在解析中,作者指出在虚继承的情况下,派生类必须明确指定如何构造虚基类,即使中间派生类已指定,最终派生类仍需重新定义。由于Derived类未指定BaseBase的构造方式,编译器将使用默认无参构造函数。博客还提供了相关笔试题的参考资料链接。
摘要由CSDN通过智能技术生成
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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值