封装总结:
staic ->类属性和类方法,这样就与类进行了绑定了
没有this指针,跟着类走的
static int s_count;
int People:😒_count=123;//此时才分配内存空间
私有化构造函数,设置静态函数来调用构造方法
:安全,降低内存管理的难度(创建时可以放入一个内存池进行管理)
class Mysort{
static void sortInsert(){
cout<<"1223"<<endl;
}
static void sortQuick(){
cout<<"1223"<<endl;
}
static void sortInsert(){
cout<<"1223"<<endl;
}
static void sortSelect(){
cout<<"1223"<<endl;
}
}
const 修饰类成员属性,成员方法,修饰静态成员,修饰成员函数,
string &getName{}const 不能对类this对象修改
void addAge()const{
this->age=18//报错
}
声明为mutable int age后可进行修改
int*p=(int*)&(this->age);//拿到对应的指针进行强制修改也可
*p=123;
const 不能修饰static方法
const 可以在声明时直接对变量初始化
const static int count =0;//全局常量
可以在const 方法修改staic
A a=1;//1->A//这里会出现隐士转换
A b(1);A c(1.0);//这里会出现类型转换
//这里可以在构造函数 A(int n): _n(n){}前面加上explicit(显嗲式调用) 取消隐式转换
RVO(返回值优化):
C++的一种编译优化技术:
可以省略函数返回过程中复制构造函数的多余调用
解决“C++中临时对象的效率问题”
Class A{
private :
int _n;
public:
A(){
cout<<"default constructor"<<endl;
};
A(int n) :_n(n){
cout<<"int constructor! address ="<<this<<endl;
}
A(const A&a){
cout<<"copy constructor address ="<<this<<endl;
}
~A(){
cout<<"desctructor! address ="<<this<<endl;
}
void operator =(const A&a){
cout<<"operator=!!"<<endl;
}
};
A func(){
A temp(123);
return temp;
}
//g++ -fno-elide-constructors a.cpp
int main(){
A a=func();
//A a(123)
// A b=a;//拷贝构造函数
//A b(a);
/*
A a(123);
A b;//此时已经完成了构造
b=a;//这里是赋值
*/
return 0;
}
Class A{
private :
int _n;
public:
A(){
cout<<"default constructor"<<endl;
};
A(int n) :_n(n){
cout<<"int constructor! address ="<<this<<endl;
}
A(const A&a){
cout<<"copy constructor address ="<<this<<endl;
}
~A(){
cout<<"desctructor! address ="<<this<<endl;
}
void operator =(const A&a){
cout<<"operator=!!"<<endl;
}
};
class B{
private:
A da;
A*pA;
public:
//参数的初始化列表效率高
B(A*a):da(*a),pA(new A(*a)){
cout<<"B copy constructor111"<<endl;
}
B(A a){
cout<<"B copy constructor222"<<endl;
this->da=a;
this->pA=new A(a);
}
};
A func(){
A temp(123);
return temp;
}
//g++ -fno-elide-constructors a.cpp
int main(){
A a;
B b=&a;
cout<<"======="<<endl;
B c=a;
cout<<"======="<<endl;
//A a=func();
//A a(123)
// A b=a;//拷贝构造函数
//A b(a);
/*
A a(123);
A b;//此时已经完成了构造
b=a;//这里是赋值
*/
return 0;
}
内存泄漏检测:
//memcheck.h
#ifndef _MEMCHECK_H_
#define _MEMCHECK_H_
#include <iostream>
#include<unordered_map>
struct MemInfo{
MemInfo(const char*mFile,unsigned long mLine,unsigned long mSize)
:file(mFile),line(mLine),size(mSize)
{
}
std::string file;
unsigned long line;
unsigned long size;
};
void* operator new(size_t size, const char* file, unsigned long line);
void* operator new[](size_t size, const char* file, unsigned long line);
void operator delete(void* ptr);
void operator delete[](void* ptr);
//有在这个宏定义会调系统里的new函数
#ifndef _NEW_OVERLOAD_IMPLEMENTATION_
#define new new(__FILE__, __LINE__)
#define delete delete(__FILE__, __LINE__)
#endif
class MemCheck{
private:
std::unordered_map<void*,MemInfo*>MemPool;
unsigned long totalMemSize;
static MemCheck memcheck;
static bool checkStatus;
public:
~MemCheck();//在析构函数中输出泄露内存信息
//保存内存信息
static void setMemInfo(void* p, const char* file, unsigned long line,
unsigned long size);
//删除释放内存信息
static void deleteMemInfo(void* p);
};
#endif
//memcheck.cpp
#define _NEW_OVERLOAD_IMPLEMENTATION_
#include "memcheck.h"
void *operator new [](size_t size,const char*file,unsigned long line){
if(size==0)size=1;
void *ptr=malloc(size);
if(ptr==nullptr){
std::cout<<"ERROR New[]"<<std::endl;
}else{
MemCheck::setMemInfo(ptr,file,line,size);
}
return ptr;
}
void operator delete[](void*ptr){
MemCheck::deleteMemInfo(ptr);
if(ptr) free(ptr);
}
void* operator new(size_t size,const char*file,unsigned long line){
if(size==0)size=1;
void *ptr=malloc(size);
if(ptr==nullptr){
std::cout<<"ERROR New"<<std::endl;
}else{
MemCheck::setMemInfo(ptr,file,line,size);
}
return ptr;
}
void operator delete(void*ptr){
MemCheck::deleteMemInfo(ptr);
if(ptr) free(ptr);
}
bool MemCheck::checkStatus=true;
std::unordered_map<void*,MemInfo>MemCheck::MemPool;
MemCheck MemCheck:: memcheck;
Memcheck ::MemCheck(){
std::cout<<"======MemCheck constructor====="<<std::endl;
}
Memcheck ::~MemCheck(){
checkStatus=false;
std::cout<<"========MemCheck destructor====="<<std::endl;
if(MemPool.size()>0){
std::cout<<"--------------leak memory info begin-------"<<std::endl;
for(auto it:MemPool){
std::cout<<"Leak Memory Address ["<<it.first<<"]";
std::cout<<"File ["<<it.second.file<<"]";
std::cout<<"Line ["<<it.second.line<<"]";
std::cout<<"Size ["<<it.second.size<<"]";
std::cout>>std::endl;
//delete it.second;
}
std::cout<<"------------------leak memory info end---------"<<std::endl;
}
}
oid MemCheck::setMemInfo(void*p,char*file,unsigned long line,unsigned long size){
if(!checkStatus)return ;
std::cout<<"MALLCO Address ["<<p<<"],";
std::cout<<" size["<<size<< "]";
std::cout<<std::endl;
MemInfo pInfo(file,line,size);
memcheck.totalMemSize+=size;
memcheck.MemPool[p]=pInfo;
std::cout<<"Total Memory Size ="<<memcheck.totalMemSize<<std::endl;
}
void MemCheck::deleteMemInfo(void*p){
if(!checkStatus)return ;
std::cout<<"FREE Address ["<<p<<"],"<<std::endl;
//std::cout<<" size["<<size<< "]";
//std::cout<<std::endl;
auto pInfo= memcheck.MemPool.find(p);
if(pInfo!=memcheck.MemPool.end()){
memcheck.totalMemSize-=pInfo->second.size;
std::cout<<"Total Memory Size ="<<memcheck.totalMemSize<<std::endl;
// delete pInfo->second;
memcheck.MemPool.erase(pInfo);
}
// totalMemsize-=(pInfo->second).size;
}
//main.cpp
#include<iostream>
#incclude "memcheck.h"
using namespace std;
class A{};
int main(){
A*p=new A();
int *n=new int[401];
int*p=new char[400];
delete[] p;
cout<<sizeof(A)<<endl;
return 0;
}