auto_ptr 是个 pointer-like 对象,也就是所谓的 “智能指针”,其析构函数会自动调用。
std::auto_ptr<Investment> ptr1(new Investment()); //之后,如果: std::auto_ptr<Investment> ptr2(ptr1); //或者: ptr2=ptr1; //那么 ptr2 指向那个对象,ptr1 置空。
shared_ptr 类似,但它可以追踪有多少指针指向该对象,并在无指针指向时析构。
这时的拷贝:
ptr2=ptr1;
结果则是两个指针指向同一对象。
auto_ptr 和 shared_ptr 一般都不要指向数组,否则它们只会析构掉数组首元素!
类数组和数组动态申请:
类数组:
class A{
public:
A():a(0){
cout<<a;
}
A(int t):a(t){
cout<<a;
}
int a;
};
void main(){
int p=3;
A arr[2]={A(),A()};
A brr[2]={A(1),A(2)};
int n=1;
A *a = new A[n];
}
数组的动态申请:
int n;
cin>>n;
float *ar = new float[n];
//二维的:
int **a=new int *[3];int i,j;
cout<<"请输入 3*3 矩阵: ";
for(i=0;i<3;i++){
a[i]=new int[3];
for(j=0;j<3;j++)
cin>>*(a[i]+j);
}
返回引用类型的函数:
/*关于引用类型返回值之函数的使用*/
#include<iostream>
using namespace std;
int g=0;
int& func(){
return g;
}
void main(){
int t=func();
cout<<t<<endl;
func()++;
cout<<g<<endl;
func()=30;
cout<<g<<endl;
}
总结几种数据类型转换的实现:
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
void main(){
//int 和 char*互转:
//int 型转 char*型,保持 int 数值显示等于结果字符串,如 13 转成"13"
int _int=123;
char _char[10];
itoa(_int,_char,10);
printf("%s ",_char);
//itoa() 函数有 3 个参数:第一个参数是要转换的数字,第二个参数是目标字符串,
//第三个参数是转移数字时所用 的基数。
//char*型转 int 型:
int _int2=atoi(_char);
printf("%d ",_int2);
//如果使用 int _int2=(int)_char,则是_char 的地址变成 int
//而如果使用 int _int2=(int)(_char),则是这句话是把_char[0] 变成 asc 码的 int
//char*和 string 互转:
//char*转 string:
string s1=(string)_char;
string s2=_char;
//这两种方法都是可以的
//string 转 char*,两个方法
const char* _char2=s1.c_str();
printf("%s ",_char2);
const char* _char3=s1.data();
printf("%s ",_char3);
//string 和 int 互转:
//string 转 int,没有直接的好方法,这里给两个方便的,其实就是先转成 char*,再转成 int
int _int4=atoi(s1.data());
int _int5=atoi(s2.c_str());
printf("%d ",_int4);
printf("%d ",_int5);
//int 转 string,也同理,就不写了
}
多态 (Polymorphism) 和对象切割 (Object Slicing) 的小例子:
#include <iostream>
using namespace std;
class Grandfather{
public:
virtual void display()=0;
void run(){
cout<<"Grandfather Run!!! ";
}
};
class Father:public Grandfather{
public:
int fatherValue;
void display(){
cout<<"Father Display!! ";
}
void run(){
cout<<"Father Run!!! ";
}
};
class Uncle:public Grandfather{
public:
int uncleValue;
void display(){
cout<<"Uncle Display!! ";
}
void run(){
cout<<"Uncle Run!!! ";
}
};
class Son:public Father{
public:
int sonValue;
void display(){
cout<<"Son Display!! ";
}
};
void main(){
Grandfather* grandfather_pt=NULL;
Father* father_pt=NULL;
Son* son_pt=NULL;
Father father;
Uncle uncle;
Son son;
cout<<"静态绑定 不用 virtual 关键字: ";
grandfather_pt=&uncle;
grandfather_pt->run();
grandfather_pt=&father;
grandfather_pt->run();
cout<<" 动态绑定 纯虚函数: ";
grandfather_pt=&uncle;
grandfather_pt->display();
grandfather_pt=&father;
grandfather_pt->display();
cout<<" 指针强制转换 ";
( (Father*)(&son) )->display();
// ( (Father*)(&son) )->run();
cout<<" 对象强制转换 ";
((Father)son).display(); //只允许 upcasting,不允许 downcasting
cout<<" 编译器为了防止对象切割的发生,自动调用拷贝构造函数,因此,比较地址: ";
cout<<( father_pt=&((Father)son) )<<endl; //注意优先级
cout<<&son<<endl;
cout<<"发现二者不同! ";
}
文章未经特殊标明皆为本人原创,未经许可不得用于任何商业用途,转载请保持完整性并注明来源链接 《四火的唠叨》