日志20191201(1)

#include <iostream>
using namespace std;

#if 1
/*
sudo apt-get *** 
允许安装的那个用户有root权限

sudo apt-get update    获取更新资源信息,没有更新
sudo apt-get upgrade   真正的更新
who 有谁登录当前机器
pwd 当前路径

初始化root
sudo passwd

设置当前时区时间
tzselect 选Asia亚洲4 中国9 北京1 Yes1 
然后cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
date -R 显示当前时间及时区

stat aaa.txt 查看文件的详细信息

!! 执行上一次命令

more 文件名		空格翻页
tail 文件名 -n		看最后n行	//对应的head

ps -ef	显示正在运行进程的详细信息
ps algf		ps aux
top		显示性能情况,类似资源管理器查看
htop	类似功能,带颜色显示
ps aux | grep kyp	字符kyp的进程		//grep 文件过滤
kill -s 9 进程id	强制杀死进程

grep a*b aaa.txt	//含字符a*b的行,*是任意0个或多个字符

df -h  	查看磁盘空间 

echo $PATH	环境变量
printenv	所有环境变量,可以套用grep查找
set			当前用户环境变量



*/

//+++++++++++++++++++++++++
//区分到底是否为空类
class myEmpty {

};

class mycharEmpty {
public:
	char data;
};

void test1() {
	cout << "sizeof(myEmpty) = "<<sizeof(myEmpty) << endl;
	cout << "sizeof(mycharEmpty) = "<<sizeof(mycharEmpty) << endl;
	//这两个类大小都是1,无法区分是空类还是有1个char,struct同class
}

template <typename T>
class EmptyClass1 : T {
	int data;
};

class EmptyClass2 {
	int data;
};

template <typename T>
bool isEmptyClass() {
	cout << sizeof(EmptyClass2) << " " << sizeof(EmptyClass1<T>) << endl;
	return sizeof(EmptyClass2) == sizeof(EmptyClass1<T>);
}

void test2() {
	bool r1 = isEmptyClass<myEmpty>();
	if (r1) {
		cout << "myEmpty是空类" << endl;
	}
	bool r2 = isEmptyClass<mycharEmpty>();	
	if (r2) {
		cout << "mycharEmpty是空类" << endl;
	}
}


//+++++++++++++++++++++++++
namespace A {
	struct X {};
	struct Y {};
	void f(int){
		cout << "A::f(int)" << endl;
	}
	void g(X) {
		cout << "A::g(X)" << endl;
	}
}

namespace B {
	void f(int i) {
		f(i);
	}
	void g(A::X x) {
		//g(x);		//函数重载不明确
	}
	void h(A::Y y) {
		h(y);
	}
}


//+++++++++++++++++++++++++
//关于内存泄漏问题
//子类析构函数要释放的成员变量要写到最后面
class CA {
public:
	explicit CA(int a) : pval(new int(a)) {
		cout << "C(int a)" << endl;
	}
	~CA(){
		cout << "~CA()" << endl;
		delete pval;
	}
private:
	int* pval;
};

class Cthrow {
public:
	Cthrow() {			//构造时抛出异常
		cout << "Cthrow()" << endl;
		throw 10;
	}
	~Cthrow() { cout << "~Cthrow()" << endl; }
};

class CN1 : public CA {
public:
	explicit CN1(int a) : CA(a),m_a(a+1),m_r(new int(a+2)),m_ct(){
		cout << "CN1()" << endl;
	}
	~CN1() {
		delete m_r;
	}
private:
	CA m_a;
	int* m_r;
	Cthrow m_ct;
/*
	CN1类在构造的时候会发生内存泄漏,整体构造顺序先父类,再成员,后自己
	成员变量顺序为列表顺序 CA int* Cthrow
	当Cthrow构造时,int* m_r已经申请了堆空间,Cthrow抛出异常,则会造成内存泄漏

	如果改成以下:
	private:
		CA m_a;
		Cthrow m_ct;
		int* m_r;
	则不会发生内存泄漏
	子类析构函数要释放的成员变量要写到最后面,即int* m_r要写到最后面。
*/
};

void test4() {
	int a = 5;
	CN1 cn(a);
}

int main() {
	//test1();	//无法判断是否为空类
	//test2();	//可以判断为空类

	test4();

	system("pause");
	return 0;
}


#endif



#if 0
//20191130
int func1(int a) {
	cout << "func1 a = " << a << endl;
	return ++a;
}

int func2(int a) {
	cout << "func2 a = " << a << endl;
	return --a;
}

void myprint(int a1, int a2) {
	cout << "a1 = " << a1 <<", "<< "a2 = " << a2 << endl;
}

int main()
{
	int a = 10;
	myprint(func1(++a), func2(a--));	//先执行func2后执行func1
	system("pause");
	return 0;
}
#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值