【目录】
一、 nullptr 与 NULL 2
1、 NULL 2
2、 nullptr 2
二、 Const对象 2
1、 被const修饰的对象,不能引用非const函数(mutable除外); 2
三、 操作符重载 3
1、 << 操作符 3
2、 >> 操作符 3
3、 ++ 前置操作符 3
4、 ++ 后置操作符 3
5、 重载下标 4
四、 二级指针 6
1、 一级指针 6
2、 二级指针 6
3、 三级指针 6
一、nullptr 与 NULL
1、NULL
①.是c语言的空指针;
2、nullptr
①.是c++的空指针;
#include<iostream>
void go( int num ){
std::cout<<"go"<<std::endl ;
}
void go( void *p ){
std::cout<<"p"<<std::endl ;
}
int main( int argc , char** argv ) {
go(nullptr) ; ---> 输出:p
go(NULL) ; -----> 输出:go
go( (void*)0 ) ;-----> 输出:p
system("pause") ;
return 0 ;
}
(注:之所以出现这样的情况,必须查看NULL在c++源码中的定义,源码中定义了在c语言环境下,NULL是一个空指针,在C++环境下是一个整数0)
二、Const对象
1、被const修饰的对象,不能引用非const函数(mutable除外);
(注:被const修饰的函数,const是放在函数后边的)
#include<iostream>
class my {
private :
int x ;
mutable int y ;
public :
my():x(10),y(20){}
void go() const{
std::cout<<"go"<<std::endl ;
}
void run(){
std::cout<<"run"<<std::endl ;
}
void hao() {
std::cout<<"hao"<<std::endl ;
}
} ;
int main( int argc , char** argv ) {
const my m1 ;
m1.go() ;
m1.run() ;//这里编译不了,原因上面说了
system("pause") ;
return 0 ;
}
三、操作符重载
1、<< 操作符
2、>> 操作符
3、++ 前置操作符
4、++ 后置操作符
#include<iostream>
using namespace std;
class my {
private :
int x ;
int y ;
public :
my():x(10),y(20){}
friend ostream & operator << ( ostream & out , my & m) ;
friend istream & operator >> ( istream & in , my & m) ;
friend my operator ++( my & m) ;
void operator ()( int num ) ;
my & operator =( const my & m ){
this->x = m.x ;
this->y = m.y ;
return *this ;
}
my operator ++(int size){ //d++ ;
my temp = *this ;
++(*this) ;
return temp ;
}
void operator <<( const int num ) /* 类的<<操作符重载,m<<4 */{
this->x += num ;
this->y += num ;
}
void operator >>( const int num ) /* 类的>>操作符重载,m>>4 */ {
this->x = num ;
this->y = num ;
}
} ;
ostream & operator << ( ostream & out , my & m) /*全局<<操作符重载*/{
out << "output:" <<m.x<<"+"<<m.y<<"i"<<std::endl ;
return out ;
}
istream & operator >> ( istream & in , my & m) /*全局>>操作符重载*/{
cout<<"input"<<endl ;
in >> m.x ;
in >> m.y ;
cout << "output:" <<m.x<<"+"<<m.y<<"i"<<endl ;
return in ;
}
my operator ++( my & m) /*类my前置++操作符重载,++m*/{
m.x++ ;
m.y++ ;
return m ;
}
void my::operator()(int num ) {
this->x = num ;
this->y = num ;
}
int main( int argc , char** argv ) {
my m1;
cout<<m1<<endl ;/* 10+20i */
m1(2) ;
cout<<++m1<<endl ;/* 3+3i */
cout<<m1++<<endl ;/* 3+3i */
m1 << 4 ;
cout<< m1 <<endl ;/* 8+8i */
m1 >> 4 ;
cout<< m1 <<endl ;/* 4+4i */
system("pause") ;
return 0 ;
}
(注:起始在类外重载的操作符比较普遍,关键看怎么用)
5、重载下标
#include<iostream>
using namespace std;
class my {
private :
int x ;
int y ;
my* mm ;
public :
my():x(10),y(20) {}
friend ostream & operator << ( ostream & out , my & m) ;
friend istream & operator >> ( istream & in , my & m) ;
friend my operator ++( my & m) ;
void operator ()( int num ) ;
my & operator =( const my & m ){
this->x = m.x ;
this->y = m.y ;
return *this ;
}
my operator ++(int size){ //d++ ;
my temp = *this ;
++(*this) ;
return temp ;
}
void operator <<( const int num ) /* 类的<<操作符重载,m<<4 */{
this->x += num ;
this->y += num ;
}
void operator >>( const int num ) /* 类的>>操作符重载,m>>4 */ {
this->x = num ;
this->y = num ;
}
my& operator []( int num ) {
cout<<"good"<<endl ;
return mm[num] ;
}
void set( my* mm) {
this->mm = mm ;
}
void show() {
cout<<"out"<<endl ;
}
} ;
ostream & operator << ( ostream & out , my & m) /*全局<<操作符重载*/{
out << "output:" <<m.x<<"+"<<m.y<<"i"<<std::endl ;
return out ;
}
istream & operator >> ( istream & in , my & m) /*全局>>操作符重载*/{
cout<<"input"<<endl ;
in >> m.x ;
in >> m.y ;
cout << "output:" <<m.x<<"+"<<m.y<<"i"<<endl ;
return in ;
}
my operator ++( my & m) /*类my前置++操作符重载,++m*/{
m.x++ ;
m.y++ ;
return m ;
}
void my::operator()(int num ) {
this->x = num ;
this->y = num ;
}
int main( int argc , char** argv ) {
/*my m1 ;
my *p = new my[2] ;
p[0](222) ;
p[1](333) ;
m1.set( p ) ;
m1[0] ;*/
my *p = new my[2] ;
p[0](222) ;
p[1](333) ;
p->set(p) ;
cout<<(*p)[0]<<endl ;
system("pause") ;
return 0 ;
}
四、二级指针
1、一级指针
①.Int a[5] ; int *p =a ;
②.int *p = new int[5] ;
2、二级指针
①.Int *a[5] ; int **p = a ;
②.int *p = new int [5] ;
③.Int a[2][3] ; int (*p)[3] = a ;
3、三级指针
①.int a[2][5] ; Int (*p)[5] = a ;(p[i][j]里面存放的是地址)
类比:
Int *a[2] ; a[0] = new int(2) ; a[1] = new int(3) ;