2017.2.20

1.类

性质关键字:protected:类和派生类可以使用

内联,重载,常成员函数不能修改,mutable不受不受const成员函数影响。

构造函数:可以重载,没有返回值。内嵌类,子类先构造后析构,母类后构造先析构

类里的引用和常量,要在构造函数中初始化。

类的外部const属性去不掉,从代码区常量表中读,类中的const可以强制转换去掉,从内存中读取。

拷贝构造函数:default和delete用法

  1 //delete可以禁用默认生成的函数,禁用构造可以无法实例化
  2 //禁用拷贝构造,可以实现禁止别人拷贝你
  3 //default默认存在
  4 class myclassA
  5 {
  6 public:
  7 	//myclassA::myclassA(void)”: 尝试引用已删除的函数
  8 	//  myclassA() = delete;//默认删除构造函数,无法实例化
  9 	//myclassA() = default;//默认存在
 10 	//myclassA(const 	myclassA &) = delete;//拷贝构造函数
 11 	//myclassA(const 	myclassA &) = default;
 12 	//=
 13 	~myclassA();
 14 };

深拷贝和浅拷贝

  1 string(const string & string1)
  2 	{
  3 		//qian
  4 		//this->p = string1.p;
  5 		//	this->length = string1.length;
  6 		//shen
  7 		this->p = new char[string1.length];
  8 		this->length = string1.length;
  9 		memset(this->p, 0, this->length);//
 10 		strcpy(this->p, string1.p);
 11 	}

静态成员变量:不能在类中初始化,不属于对象变量,可以不借助实例类对象访问,可以用于计数。

静态成员函数:没有this指针,不属于某对象变量,静态成员函数不可以调用类的非静态成员,

  1 void(myclass::*p1)() = &myclass::run;
  2 //代码共享,所有的类对象共享对象,
  3 void(*p2)() = &myclass::go;//静态函数,与对象没有关系

静态常量:

  1 class A
  2 {
  3 public  4 static const int dashu;
  5 }
  6 const int myclass::dashu = 20;//常量
  7 
  8 void  main()
  9 {
 10 	const int *px = &(myclass::dashu);
 11 	std::cout << px << std::endl;
 12 	int *p = const_cast<int *> (px);
 13 	*p = 123;//静态常量区可以访问,不可以修改,
 14 	std::cout << *px << "  " << *p << "   " << myclass::dashu;
 15 
 16 	std::cin.get();
 17 
 18 
 19 }

友元函数:

独立的,友元函数可以访问类的私有变量,还可以访问私有函数

友元函数声明有friend,定义就不需要了或者定义在内部
友元类:
不适用于继承,只适用于当前类,
 

2.union共同体大小由其中最大类型的决定,始终只能有一个变量,不能继承,成员都是公有的,不能修改。

  内部数据是共享的,对象之间是独立的,代码是共享的。

3.等号和括号的区别

4.explicit  避免歧义,误认为可以类型转换

posted @ 2017-02-20 22:14  acliang  阅读(150)  评论(0编辑  收藏  举报