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

 
 

面向对象的下篇侧重于C++的技术主线-泛型编程(Generic Programming)和面向对象编程(Object-Oriented Programming)。然后通过深入探索C++继承关系的对象模型,描述底层this指针,vptr(虚函数),vtbl(虚表),virtual mechanism(虚机制)以及 virtual function(虚函数)形成 polymorphism (多态)的成因。

Operator Overloading

Operator Overloading 在C++中是一个非常重要的特性,很多的C++中非常重要的特性都是由此产生的,比如 conversion function, functor(function-like class), smart pointer(pointer-like class)。

Operator Overloading有以下的一些类型

operator op
operator type
operator new
operator new []
operator delete
operator delete []
operator "" suffix-identifier (since C++11)
user-defined conversion function

转换函数是能够隐式或者显式的把一个class类型转成其它的类型。可以像成员函数一样声明,但是没有返回类型的(一般需要定义为const函数)。

syntax

operator conversion-type-id
explicit operator conversion-type-id  (since c++11)

其中 conversion-type-id 是 除去 函数和数组的操作符 [] ()的 类型(如 bool, int 等)

class Fraction 
{
public:
    Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) {}
    operator double() const { return (double)(m_numerator / m_denominator); }
private:
    int m_numerator;
    int m_denominator;
};

// test
Fraction f(3, 5)
double d = 4 + f // 编译器将调用 operator double 将f 转成 double
functor(function-like class)

functor 表示 class具有function 的特性。 class 通过重载 () 实现。

template <class T>
struct identity {
    const T& operator() (const T& x) const { return x; }
};

// test
identity<int> t();
int t2 = t(); // 编译器将调用 operator()
pointer-like class

pointer-like class 最常用的就是智能指针和迭代器。 class 通过重载 *-> 实现指针的表现功能。

template<class T>
class shared_ptr
{
public:
    T& operator*() const { return *px; }
    T* operator->() const { return px; }
private:
    T* px;
    long* pn;
};

// test
shared_ptr<Foo> sp(new Foo);
Foo f(*sp);   // 将调用 operator*() 

sp->method(); // 将调用 operator->(). -> 符号有个特性就是作用完之后还会继续存在

template

模版是C++ 中实现泛型编程的一个最重要的手段。

class template

类模版中可以指定类中函数参数或者成员变量的类型为模版类型。

template<typename T>
class Complex
{
public:
    complex (T r = 0, T i = 0) : re(r), im(r) { }
private:
    T re, im;
};
function template
template<class T>
inline const T& min(const T& a, const T& b)
{
    return b < a ? b : a;
}
member template
template sepecialization

模版的特化的就是指定模版参数的类型。

Syntax

template <> declaration

template<>
struct hash<char> {
    size_t operator() (char x) const { return x; }
}
template partial sepectialization

模版的偏特化就是固定模版参数的一些类型。

个数的偏:

template<typename T, typename Alloc=std::alloc>
class vector
{
...
};

template<typename Alloc=std::alloc>
class vector<bool, Alloc>
{
...
};  // 这种写法就是上面的偏特化,固定了其中的一个模版参数类型,只有一个模版参数

范围的偏

template<typename T>
class C
{
};

template<typename T>
class C<T*>
{
...
}; // 这种写法就是上面的偏特化,固定了参数的模版类型(指针类型)
template template parameter

模版模版参数

c++11 的一些语言特性

c++11 增加了很多新的语言特性,这些特性更佳丰富了C++的语法,更加具有现代编程语言的特性。

__cplusplus 宏,

c++ 98/03 的 __cpulsplus 为 199711, C++ 11 的为 201103

auto

自动类型推导,C++11 中的 auto不同于 以前的 auto 变量的语义(局部变量)。C++ 11 中auto 可以让编译器自动帮助去推导出左值变量的类型。

list<string> c;
...
auto ite = find(c.begin(), c.end(), target); // 编译器可以自动推导出 ite 的类型
variadic templates(数量不定的模版参数)

C++11 提供任意个数模版参数的语法 ...

range-base for

一种更加简单方便的遍历容器中元素的写法。

syntax

for (range_declaration : range_expression) loop_statement

range_declaration: 命名变量的声明, 类型是 range_expression 表示的 序列中的元素类型, 或者是这种元素类型的引用。 一般可以使用auto (或 auto& )去表示。

range_expression: 一段适合的序列表达式(通常是 一个数组或者一个拥有 begin 和 end 成员函数的对象) 或者是由 花括号括起的初始值序列。

类似的还有for_each 的语法。

std::vector<int> v = {0, 1, 2, 3, 4, 5};
 for(const int &i : v) // access by const reference
        std::cout << i << ' ';
    std::cout << '\n';

    for(auto i: v) // access by value, the type of i is int
        std::cout << i << ' ';
    std::cout << '\n';

    for(auto&& i: v) // access by reference, the type of i is int&
        std::cout << i << ' ';
    std::cout << '\n';

    for(int n: {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list
        std::cout << n << ' ';
    std::cout << '\n';

    int a[] = {0, 1, 2, 3, 4, 5};
    for(int n: a)  // the initializer may be an array
        std::cout << n << ' ';
    std::cout << '\n';

    for(int n: a)  
        std::cout << 1 << ' '; // the loop variable need not be used
    std::cout << '\n';



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值