A static member variables and functions
- Added static is static member variables, share data
- Static variables declared within the class members, the class is initialized outside.
- Static variables also privileged member
- Static member function can not access ordinary variables
- Static member function is privileged
Second, singleton
- To give only one instance of the class, instance, you do not need to release
- The default constructor and copy constructor privatization
- Internal maintain a pointer to the object
- Privatization unique pointer
- Provide external getinstance way to access the pointer.
Third, the object model
- 1 empty class size, each object instance has a unique address. char maintain this address.
- Only non-static member attribute only belong to the object
- this pointer always points to the current object, to resolve the current conflict
- If the member function does not use this, then the null pointer can be accessed directly,
- If you use this member function pointers, we should note that if the judge can add
Fourth, often function normally objects
Constant function
- void func() const{}
- Modification is this pointer, const type * const this
- Modify the value of this pointer is not performed
Often the object
- Before modifying the object join const const Person p1
- Can not call an ordinary member function can be called constant function
- mutable keyword can be modified in the modified constant function
Fifth, friend
Friendship is a one-way
global functions do friend ----> The purpose is to visit the class of private property, friend
like to do friend ----> friend class
Six, operator overloading
- If you want custom data type for the + operator, then the need to reload the + operator.
//修改加号运算符
class Person
{
public:
Person() {};
Person(int a,int b):m_A(a),m_B(b){};
Person operator+ (Person &p) //成员函数重载运算符
{
Person tmp;
tmp.m_A = this->m_A + p->m_A;
tmp.m_B = this->m->B + p->m_B;
}
int m_B;
int m_A;
};
//利用全局函数进行运算符重载
Person operator(Person &p1,Person &p2)
{
Person tmp;
tmp.m_A = this->m_A + p->m_A;
tmp.m_B = this->m->B + p->m_B;
}
Person p3 = p1+p2;
- Left shift operator overloading can not write ye member function, global function can only be used
ostream& operator(ostream &cout, Person &p1) //引用返回
{
cout << p1.m_a << p1.m_b; //当m_a为私有时,需要加友元friend
return cout;
}
void testFunc(int &ref) //发现是引用,转换为int *const ref = &a
{
ref = 10; //ref 是引用,转换成*ref = 10;
}
- Pre- and post-operator
//前置++
MyInter operator++()
{
this->num++;
return *this;
}
//后置++
MyInter operator++(int) //int 为占位参数,区分前后置
{
//临时保存目前数据
MyInter tmp = *this;
num++;
return tmp;
}
- Smart pointers, used to host the type of custom objects, so that objects are automatically released.
class Smart
{
Smart pointer(Smart *pointer)
{
this->pointer = pointer;
}
private:
Smart *pointer;
};
void test()
{
Smart s(new Person(10))
s->showage //重载->运算符
}
- = Reload
- A default class to create a default constructor, destructor, copy construction, operate = simple assignment operator pass values