问题1: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()
{
//malloc函数申请的内存不能初始化,new关键字申请的内存可以初始化
int* p = reinterpret_cast<int*>(malloc(sizeof(int)));
int* q = new int(10);
*p = 5;
//*q = 10;
cout<<*p<<" "<<*q<<endl;
free(p);
delete q;
//new和delete还负责构造函数和析构函数的调用,malloc和free只是单纯的负责内存申请与释放
Test* op = reinterpret_cast<Test*>(malloc(sizeof(Test)));
cout<<"================"<<endl;
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;
}
编译运行结果如下:
1. malloc和free是库函数,以字节为单位申请堆内存;
2. new和delete是关键字,以类型为单位申请堆内存;
3. malloc和free单纯的对内存进行申请与释放;
4. 对于基本类型new关键字会对内存进行初始化;
5. 对于类类型new和delete还负责构造函数和析构函数的调用;
问题2:编译器对构造函数的调用?
#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()
{
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;
}
编译运行结果如下:
有没有发现效率比较低? 执行Test t2 = 5; 先生成一个临时对象,然后在调用拷贝构造函数,用之前生成的临时对象对t2进行初始化。之前老的C++编译器就是这么干的。新的C++编译器进行了改进。
问题3:C++中explicit关键字的使用
“剥夺”编译器对构造函数的调用尝试。C++提供了explicit关键字用于阻止编译器对构造函数的调用尝试。
#include <cstdlib>
#include <iostream>
using namespace std;
class Test
{
public:
explicit Test(int i)
{
cout<<"Test(int i)"<<endl;
}
Test(const Test& obj)
{
cout<<"Test(const Test& obj)"<<endl;
}
~Test()
{
cout<<"~Test"<<endl;
}
};
void func()
{
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;
}
编译运行结果如下:
问题四:类的静态成员能用来干嘛呢?
用于对象数目控制。一个类最多只能有一个对象存在于系统中,如何实现?通过单例模式实现。
#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();
}
return cInstance;
}
void print()
{
cout<<"I'm Singleton!"<<endl;
}
};
Singleton* Singleton::cInstance = NULL;
void func()
{
Singleton* s = Singleton::GetInstance();
Singleton* s1 = Singleton::GetInstance();
Singleton* s2 = Singleton::GetInstance();
//Singleton* s3 = new Singleton();
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;
}
编译运行结果如下:
问题5:无状态函数和状态函数
无状态函数:函数的调用结果只与实参值相关;
状态函数:函数的调用结果不仅与实参值相关还与之前的函数调用有关;
#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;
}
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),但是无法从头再来。
问题6:函数对象的实现
#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;
}
编译运行结果如下: