-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
malloc 与 free 和 new 与delete 有什么区别?
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
private:
int i;
public:
Test()//构造函数
{
cout<<"Test()"<<endl;
i = 0;
}
Test(int i)//构造函数
{
cout<<"Test(int i)"<<endl;
this->i = i;
}
~Test()
{
cout<<"~Test"<<endl;
}
int getI()
{
return i;
}
};
void func()
{
//申请内存空间
int* p = reinterpret_cast<int*>(malloc(sizeof(int)));//malloc 返回值是 void* 需要用到强制类型转换 不能进行初始化
int* q = new int(10);//申请后赋值初始值为10
*p = 5;
//*q = 10;//等价赋初始值
cout<<*p<<" "<<*q<<endl;
free(p);
delete q;
Test* op = reinterpret_cast<Test*>(malloc(sizeof(Test)));//op只是一段空间,并不能成为对象,不会调用构造函数和析构函数
Test* oq = new Test;//可以调用构造函数
cout<<op->getI()<<" "<<oq->getI()<<endl;
free(op);
delete oq;//调用析构函数
}
int main(int argc, char *argv[])
{
func();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
malloc 和 free 是库函数,以字节为单位申请堆内存
new 和 delete 是关键字,以类型为单位申请堆内存
malloc 和 free 单纯的对内存进行申请与释放
对于基本类型 new 关键字会对内存进行初始化
对于类类型 new 和 delete 还负责构造函数和析构函数的调用
第二:
编译器对构造函数的调用
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
Test(int i)
{
cout<<"Test(int i)"<<endl;
}
Test(const Test& obj)
{
cout<<"Test(const Test& obj)"<<endl;
}
~Test()
{
cout<<"~Test"<<endl;
}
};
void func()
{
//3种调用构造函数赋初值等价
Test t1(5);//标准
Test t2 = 5;//类型转换
Test t3 = Test(5);//拷贝构造函数
}
int main(int argc, char *argv[])
{
func();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
C++ 编译器会尝试各种手段尝试让程序通过编译
方式一:尽力匹配重载函数
方式二:尽力使用函数的默认参数
方式三:尽力尝试调用构造函数进行类型转换
(旧版本)方案 A:
Test t1 = 5; <--> Test t1 = Test(Test(5));
(现代版本)方案 B: :
Test t1 = 5; <--> Test t1(5);
“ 剥夺 ” 编译器对构造函数的调用尝试
C++ 提供了 explicit 关键字用于阻止编译器对构造函数的调用尝试
第三:
类的静态成员能用来干嘛呢?
对象数目控制
一个类最多只能有一个对象存在于系统中,如何实现?
单例模式的实现
#include <cstdlib>
#include <iostream>
using namespace std;
class Singleton
{
private:
static Singleton* cInstance;//静态指针
Singleton()//构造函数私有
{
}
public:
static Singleton* GetInstance()//静态成员函数
{
if( cInstance == NULL )//最多执行一次
{
cout<<"new Singleton()"<<endl;
cInstance = new Singleton();//在GetInstance函数内部调用成员函数合法
}
return cInstance;
}
void print()
{
cout<<"I'm Singleton!"<<endl;
}
};
Singleton* Singleton::cInstance = NULL;
void func()
{
//调用三次GetInstance
Singleton* s = Singleton::GetInstance();//s指向静态成员函数返回的值
Singleton* s1 = Singleton::GetInstance();
Singleton* s2 = Singleton::GetInstance();
cout<<s<<" "<<s1<<" "<<s2<<endl;
s->print();
}
int main(int argc, char *argv[])
{
func();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第四:
无状态函数
函数的调用结果只与实参值相关
状态函数
函数的调用结果不仅与实参值相关还与之前的函数调用有关
#include <cstdlib>
#include <iostream>
using namespace std;
int fib1(int i)//无状态函数 时间复杂度为 O(n)
{
int a1 = 0;
int a2 = 1;
int ret = a2;
while( i > 1)
{
ret = a2 + a1;
a1 = a2;
a2 = ret;
i--;
}
return ret;
}
int fib2()//状态函数 时间复杂度为 O(1)
{
static int a1 = 0;
static int a2 = 1;
int ret = a2;
int t = a2;
a2 = a2 + a1;
a1 = t;
return ret;
}
int main(int argc, char *argv[])
{
for(int i=1; i<=10; i++)
{
cout<<fib1(i)<<endl;
}
for(int i=1; i<=10; i++)
{
cout<<fib2()<<endl;
}
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
疑惑解答
两中实现的问题:
fib1 是以无状态函数的方式实现的,求解数列每一项时都会做重复的循环,时间复杂度为 O(n)
fib2 是以状态函数的方式实现的,每调用一次就可以得到数列当前项的值,时间复杂度为 O(1) , 但是无法从头再来
函数对象的实现(优化)
#include <cstdlib>
#include <iostream>
using namespace std;
int fib1(int i)
{
int a1 = 0;
int a2 = 1;
int ret = a2;
while( i > 1)
{
ret = a2 + a1;
a1 = a2;
a2 = ret;
i--;
}
return ret;
}
int fib2()
{
static int a1 = 0;
static int a2 = 1;
int ret = a2;
int t = a2;
a2 = a2 + a1;
a1 = t;
return ret;
}
class Fib
{
private:
int a1;
int a2;
public:
Fib()
{
a1 = 0;
a2 = 1;
}
int operator() ()//操作符重载
{
int ret = a2;
int t = a2;
a2 = a2 + a1;
a1 = t;
return ret;
}
};
int main(int argc, char *argv[])
{
cout<<"int fib1(int i)"<<endl;
for(int i=1; i<=10; i++)
{
cout<<fib1(i)<<endl;
}
cout<<endl;
cout<<"int fib2()"<<endl;
for(int i=1; i<=10; i++)
{
cout<<fib2()<<endl;
}
cout<<endl;
Fib fib;
cout<<"Fib fib;"<<endl;
for(int i=1; i<=10; i++)
{
cout<<fib()<<endl;//操作符重载
}
cout<<endl;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}