小米2017校招面试经历

早上九点来到广州市珠江新岸酒店进行小米的面试,下面是面试官的一些问题,虽然已经阵亡,但是还要补充自己的不足!!!!经过这次面试,个人深刻体会到,所谓的

基础就是强悍的编程能力(涉及到问题的分析和解决能力)+深入掌握一门语言(c++/java,这个可以考察你对知识了解的深入程度),这个在互联网技术岗位是非常重要的!!!!!

问题1:

dynamic_cast的作用(已回答),检测多层继承模型中类的上行转换的安全性!

问题2:

explicit关键字的作用(当时没想起来!!!)

explicit关键字在c++中是为了防止隐式转换,假设我们有一个类是这样定义的:

#include<fstream>
#include<string>
#include<cstring>
using namespace std;

class base{
public:
	base(int x){
		a = x;
		cout << "has construct!"<< endl;
	}
	~base(){
		cout << "has destruct!"<< endl;
	}
	bool is_same(base bb){
		if (a == bb.a)return true;
		else return false;
	}
	int a;
};

int main(){
	base bb(2);
	cout << bb.is_same(2) << endl;
	return 0;
}
对于非类临时对象来说,假设我们有一下语句:

int a=3;

double b=4.23456;

if(a==b)return ture;

else return flase;

在上面的语句中,double类型会被隐式转换成int类型

可能编译器会产生如下代码:

int a=3;

double b=4.23456;

int temp=b;

if(a==temp)return ture;

else return flase;

中途会产生一个临时变量,这样就会有一个隐式转换,一般而言隐式转换会伴随一个临时变量的产生!!!

问题3:

如何限制一个类对象只在堆上或栈上分配(虽然没有回答上来,但个人觉得这题是最能体现对类的理解程度)

class base{
public:
	base(){
		a = 1;
		b = 2;
	}
	int a; 
	int b;
private:
	void * operator new(size_t a){
		cout << a << endl;
		cout << "this is the operator new of class!"<< endl;
		void *p = malloc(a);
		*(int *)p = 3;
		*((int *)p + 1) = 4;
		return (void *)p;
	}
};

//void * operator new(size_t a){
//	cout << a << endl;
//	cout << "this is the operator new of class!" << endl;
//	void *p = malloc(a);
//	*(int *)p = 3;
//	*((int *)p + 1) = 4;
//	cout << p << endl;
//	return (void *)p;
//}


int main(){
	base bb;
	cout << bb.a <<" "<< bb.b << endl;
	base *ptr=(base *)new base();//编译不通过
	//cout << ptr << endl;
	//base* p=(base *)ptr->operator new(8);
	//cout << ptr->a << ptr->b << endl;
	//cout << p->a << p->a << endl;
	return 0;
}
我们将在类里面重载了operator new函数,所以在new的时候就不会调用全局std::operator(size_t size)函数,只会调用类里面的重载函数,但是此时我们把operator设置为

私有,希望被new创建的对象没有权限调用私有成员函数,就会出现以下错误:

1>e:\vs2013 projects\daily_practice.cpp\daily_practice.cpp\daily_practice.cpp(41): error C2248: “base::operator new”: 无法访问 private 成员(在“base”类中声明)
1>          e:\vs2013 projects\daily_practice.cpp\daily_practice.cpp\daily_practice.cpp(17) : 参见“base::operator new”的声明
1>          e:\vs2013 projects\daily_practice.cpp\daily_practice.cpp\daily_practice.cpp(8) : 参见“base”的声明
1>
所以这个时候,就不能在堆上建立类的对象。

只能在堆上建立对象:

class base{
public:
	int a;
	int b;
	static base *create(){
		return new base();
	}
	void destory(){
		delete this;//调用析构函数
	}
private:
	base(){
		a = 1;
		b = 2;
	}
	~base(){
		cout << "destructor!"<< endl;
	}

};


int main(){
	base *p1 = base::create();
	cout << p1->a<<"  " << p1->b << endl;
	p1->destory();
	return 0;
}

这种情况只能在堆上建立!!!!


问题4:设计一个单例模式:

class base{
public:
	int a;
	int b;
	static base *create(){//设置为静态函数,可以通过类作用域访问  
		if(p==NULL)p = new base();
		return p;
	}
	void destory(){
		delete this;//调用析构函数  
	}
private:
	base(){
		a = 1;
		b = 2;
	}
	~base(){
		cout << "destructor!" << endl;
	}
	static base *p;
};
base *base::p = NULL;

int main(){
	base *p1 = base::create();
	base *p2 = base::create();
	if (p1 == p2)cout << "same!" << endl;
	p1->destory();
	return 0;
}


问题5:

编程:给定一个字符串,里面有若干空格,实现一个算法将空格用后面的字符填补,要求原地操作,可以使用少量的交换空间,例如

输入:ab#cd###ef

输出:abcdef

一共有两种方法,当时没想出来。

#include <iostream>
#include<vector>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;


int main(){
    char a[]="ab##bc##def#";
    int len=strlen(a);
    int count=0;
    int now=0;
    for(int i=0;i<len;i++){
        if(a[i]!='#'){
            count++;
        }
      }
      now=count;//count为写入位置
      for(int i=count;i<len;i++){
        if(a[i]!='#'){
          a[count++]=a[i];
        }
      }
      a[count]='\0'; 
  printf("%s\n",a);
}
第二种方法:

int main(){
	char a[] = "ab##bc##def#";
	int len = strlen(a);
	int count = 0;
	int i = 0;
	for ( i = 0; i<len; i++){
		if (a[i] == '#'){
			count++;
		}
		else{
			a[i - count] = a[i];
		}
	}
	a[i-count] = '\0';
	printf("%s\n", a);
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值