码迷,mamicode.com
首页 > 编程语言 > 详细

C++面向对象高级编程(下) 第一周笔记 GeekBand

时间:2016-05-30 23:05:39      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:

1.Conversion Function 转换函数

class Fraction{

public:

operator double() const { //转换不可能改变类里的成员,通常加上const

return (double)(m_numerator / m_denominator);

}//把Fraction对象转换成double类型,double()不可有参数,返回类型不用写

private:

int m_numerator;

int m_denominator;

};

Fraction f(3,5);

double d=4+f;//调用operator double()将f转为0.6

2.non-explict it one arguement construct

//可以把别的东西转换为这种东西

class Fraction{

public:

Fraction(int num,int den=1):m_numerator(num),m_denominator(den) {  }

operator double const {

return (double)(m_numerator / m_denominator);

}

Fraction operator+(constFraction& f) {

return

Fraction(...);

private:

int m_numerator;

int m_denominator;

};

Fraction f3,5);

Fractiond2=f+4;//调用non_explict ctor 4转为Fraction,然后调用operator+

template

calss vector//模板的先特化,容器里的每个元素都是ool值

{

public:

typedef __bit_reference reference;

protected:

reference operator[](size type n){//代表本来真正传回来的东西

return *(begin() +diffenece_type(n));

}

 

struct __bit_reference

{

unsigned int* p;

unsigned int mask;

...

public:

operator bool() const { return !(!(*p * mask));}

};

3.pointer-like classes

template

class shared_ptr{

public:

T& operator*() const

{

return *px;

}

T* operator->() const{

return px;

}

shared_ptr(T* p):px(p) { }

private:

T* px;//指向T的指针,什么类型都接受

long* pn;

};

lass Foo{

...

void method(void) {...}

};

shared_ptr sp(new Foo);

Foo f(*sp);

sp->method();//调用method()

px->method();//通过这个对象调用这个函数

箭头符号有一个特殊的行为:作用下去得到的结果,这个箭头符号会继续作用下去

智能指针里头一定带着一个一般的指针,而智能指针一定要带*->符号

point-class 这种类型

4迭代器

主要用来遍历容器

template

struct _list_iterator//链表的迭代器

{

typedef _list_iterator self;

typedef Ptr pointrt;

typedef Ref reference;

typedef _list_node* linx_type;//指向结点的指针

link_type node;

bool operator == (const self& x) const {return node == x.node;}

bool operator != (const self& x) const {return node != x.node;}

reference operator*() const {return (*node}.data;}

pointer operator-> const {return &(operator*());}

self& operator++() {node = (link_type)((*node).next);return *this;}

self operator++(int) {self tmp = *this; ++*this; return tmp;}

self& operator--() {node = (link_type)((*node).prev;return *this;}

self operator--(int) {self tmp = *this; --*this; return tmp;}

};

5 Function - like class

template

struct identity : public unart_function{

const T& operator()(const T& x) const { return x;}

};

template

struct selectst{//取出第一个

const typename Pair::first_type& operator() (const Pair& x) const

{

return x.first;

}

};

template

Struct select2nd : publicunart_function{//取出第二个

const typename Pair::second_type& operator()(const Pair& x) const

{

return x.second;

}

};

template

struct pair: publicunart_function{

T1 first;

T2 second;

pair() : first(T1()),second(T2()) {}

pair(const T1& a,const T2& b):first(a),second(b) { }

};

6.class template 类模板和 fumction template 函数模板

template//复数的类模板

class complex{

public:

complex(T r=0,T i=0):re(r),im(i) {}

complex & operator += (const complex&);

T real() const {return re;}

T imag() const {return im;}

private:

T re,im;

friend complex& __doapl(complex*,const complex&);

};

{

complex c1(2.5,1.5);

complex c2(2,6);

}

函数模板:

class stone{

public:

stone(int w,int h,int we):_w(w),_h(h),_weight(we) {}

bool operator<<(const stone& rhs) const {

return _weight < rhs._weight;

}

private:

int _w,_h,_weight;

};

template

inline const T& min(const T& a, const T& b)

{  return b;}

函数模板在使用的时候不必指明type,能够通过调用时传递的实参推出来类型

member template 成员模板

template

struct pair{

typedef T1 first_type;

typedef T2 second_type;

T1 first;

T2 second;

pair():first(T1()),second(T2()) {}

pair(const T1 &a,const T2 &b)

:first(a),second(b) {}

template   //成员模板

pair(const pait &p)//T1和T2类型确定了以后,U1和U2也能进行确定

:first(p.first),second(p.second) {}

}

7.模板特化 specialization

Template

struct hash ();

template<>

struct hash{

size_t operator() (char x) const {return x;}//重载

};

template<>

struct hash{

size_t operator() (int x) const {return x;}

};

template<>

struct hash{

size_t operator() (long x) const {return x;}

};

C++面向对象高级编程(下) 第一周笔记 GeekBand

标签:

原文地址:http://www.cnblogs.com/zhanghouyu/p/5543997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!