C++学习笔记——面向对象高级开发
P2 转换函数 conversion function
class Fraction {
public :
operator double ( ) const {
return ( double ) ( m_numerator/ m_denominator) ;
}
private :
int m_numerator;
int m_denominator;
} ;
Fraction f ( 3 , 5 ) ;
double d = 4 + f;
P3 non-explicit-one-argument constructor
class Fraction {
public :
Fraction ( int num, int den= 1 ) : m_numerator ( num) , m_denomiator ( den) { }
operator double ( ) const ;
explicit Fraction ( int num, int den= 1 ) : m_numerator ( num) , m_denomiator ( den) { }
Fraction operator + ( const Fraction& f) {
return . . . ;
}
private :
int m_numerator;
int m_denominator;
} ;
Fraction f ( 3 , 5 ) ;
Fraction d2 = f+ 4 ; ;
P4 pointer-like classes,行为像指针的类
template < class T >
class share_ptr {
public :
T& operator * ( ) const { return * px; }
T* operator - > ( ) const { return px; }
shared_ptr ( T* p) : px ( p) { }
private :
T* px;
long * pn;
} ;
struct Foo{
. . .
void method ( void ) { . . . }
} ;
shared_ptr< Foo> sp ( new Foo) ;
Foo f ( * sp) ;
sp- > method ( ) ;
迭代器:定义了++、–、*、->、==、!=等操作符
P5 function-like classes,行为像函数,仿函数
P6 namespace经验谈
P7 class template,类模板
P8 function template,函数模板
P9 member template,成员模板
template < class T1 , class T2 >
struct pair{
T1 first;
T2 second;
pair ( ) : first ( T1 ( ) ) , second ( T2 ( ) ) { }
pair ( const T1& a, const T2& b) : first ( a) , second ( b) { }
template < class U1 , class U2 >
pair ( const pair< U1, U2> & p) : first ( p. first) , second ( p. second) { }
} ;
P10 specialization,特化
template < class K > struct hash{ } ;
template < > struct hash< char > {
size_t operator ( ) ( char x) const { return x; }
}
template < > struct hash< int > {
size_t operator ( ) ( int x) const { return x; }
}
P11 partial specialization模板偏特化
template < typename T, typename Alloc= . . . >
class vector {
. . .
} ;
template < typename Alloc= . . . >
class vector < bool , Alloc> {
. . .
} ;
template < typename T>
class C {
. . .
} ;
template < typename T>
class C < T* > {
. . .
} ;
P12 template template parameter,模板模板参数
template < typename T,
template < typename T> class SmartPtr
>
class XCls {
private :
SmartPtr< T> sp;
public :
XCls ( ) : sp ( new T) { }
} ;
XCls< string, shared_ptr> p1;
XCls< double , unique_ptr> p2;
XCls< int , week_ptr> p3;
XCls< long , auto_ptr> p4;
P13 关于C++标准库
P14 三个主题
void print ( ) { }
template < typename T, typename . . . Types>
void print ( const T& firstArg, const Types& . . . args) {
cout<< firstArg<< endl;
print ( args. . . ) ;
}
auto:编译器自动推断类型 range-based for
vector< double > vec;
. . .
for ( auto e: vec) {
cout<< e<< endl;
e * = 4 ;
}
for ( auto & e: vec) {
e * = 3 ;
}
P15 引用
int x = 0 ;
int * p = & x;
int & r = x;
void func1 ( Cls* pobj) ;
void func2 ( Cls obj) ;
void func3 ( Cls& obj) ;
void func ( const int a) ;
void func ( const int & a) ;
void func ( const int a) const ;
P16 复合&继承关系下的构造和析构
继承:构造由内到外,析构由外到内 组合+继承:先构造父类,再构造组合
P17关于vptr和vtbl,虚指针、虚表
有虚函数的类就会多一个虚函数指针 动态绑定,多态 三个条件:指针调用,向上转型,调用虚函数
P18 关于this
P19 对象模型:关于动态绑定
P20 关于 new,delete
new:先分配内存,再调用构造函数 delete:先调用析构函数,再释放内存
P21 重载new,delete
inline void * operator new ( size_t size) ;
inline void * operator new [ ] ( size_t size) ;
inline void * operator delete ( size_t size) ;
inline void * operator delete [ ] ( size_t size) ;
class Foo {
public :
void * operator new ( size_t) ;
void operator delete ( void * , size_t) ;
} ;
P22 示例
P23重载new(),delete()示例
P24 basic_string试用new(extra)扩充申请量