C++ Idioms 01

  • 本文介绍了 C++ 的 5 个特别用法(Idioms),分别是
    • addressof();
    • Attorney-Client;
    • Calling Virtuals During Initialization;
    • Capability Query;
    • Clear-and-minimize;

01. addressof()

用途

  • 类可以重载 & 使其违反“取对象地址”的本意,此时可以通过 addressof() 获得对象地址

示例代码

#include<iostream>
using namespace std;

class UnAddressable
{
private:
  double operator&() const { return 0.0; } // 限制访问取地址运算符 & 
};

int main() {
  UnAddressable na;
  UnAddressable* naptr1 = &na;  // Compile error
  UnAddressable* naptr2 = addressof(na);
  cout << "Address of an UnAddressable object: " << naptr2 << endl; // 0x7fff46ffba27
  return 0;
}

02. Attorney-Client

用途

  • 提供类内实现细节的有选择性(部分)可访问
  • C++ 中类的友元 friend 可以全权访问类内的实现细节,这就破坏了封装性,但 friend 主张 “all or nothing”,无法实现对私有成员的有选择访问
  • Attorney-Client 方法

示例代码

没有继承的情况

class Client
{
  friend class Attorney;
private:
  void func1() {}
  void func2() {}
  void func3() {}
};

class Attorney
{
  friend class Bar;
private:
  static void call_func1(Client& c) { c.func1(); }
  static void call_func2(Client& c) { c.func2(); }
};

class Bar
{
  // only Client::func1() and Client::func2() is accessible (through Attorney) here
};

存在继承时

#include<iostream>
using namespace std;

class Base
{
  friend class Attorney;
public:
  virtual ~Base() {}
private:
  virtual void func() { cout << "Base::func()" << endl; } // 私有,外界不可访问
};

class Derived : public Base
{
public:
  ~Derived() {}
private:
  virtual void func() { cout << "Derived::func()" << endl; } // 私有,外界不可访问
};

class Attorney
{
  friend int main();  // 通过 Attorney 使得 main() 可以访问 Attorney 允许的成员,同时保持多态性
private:
  static void call_func(Base* base) {
    return base->func();
  }
};

int main() {
  Base* d1 = new Base;
  Attorney::call_func(d1);  // output: Base::func()
  Base* d2 = new Derived;
  Attorney::call_func(d2);  // output: Derived::func()
  return 0;
}

03. Calling Virtuals During Initialization

用途

  • 在派生类对象初始化期间调用虚函数,也称为初始化期间的动态绑定

示例代码

  • 原始情况:无法实现上述目标
class Base
{
public:
  Base() { func1(); func2(); }
  virtual ~Base() {}
  virtual void func1() const { /* do something */ }
  virtual void func2() const { /* do something */ }
};

class Derived : public Base
{
public:
  virtual void func1() const { /* do something other */ }
  virtual void func2() const { /* do something pther */ }
};
  • 解决方法
class Base
{
public:
  // Base() { func1(); func2(); }
  virtual ~Base() {}
  void init() { func1(); func2(); }
  virtual void func1() const { cout << "Base::func1()" << endl; }
  virtual void func2() const { cout << "Base::func2()" << endl; }
};

class Derived : public Base
{
public:
  Derived(const char* s) { cout << s << endl; }
  virtual void func1() const { cout << "Derived::func1()" << endl; }
  virtual void func2() const { cout << "Derived::func2()" << endl; }
};

shared_ptr<Base> factory()
{
  shared_ptr<Base> ptr(new Derived("Derived class"));
  ptr->init();
  return ptr;
}

int main() {
  shared_ptr<Base> b1 = factory(); // 同时完成派生类对象的创建和虚函数的调用
  return 0;
}

04. Capability Query

用途

  • 在运行时检查对象是否支持某个接口
  • 当一个类继承自多个基类时,可能无法预知它实现了哪个特定接口
  • 通过 dynamic_cast 进行“试探”

示例代码

class Shape
{
public:
  virtual ~Shape() {}
  virtual void draw() const = 0;
};

class Rollable
{
public:
  virtual ~Rollable() {}
  virtual void roll() = 0;
};

class Circle : public Shape, public Rollable
{
public:
  void draw() const { cout << "Drawing a circle." << endl; }
  void roll() { cout << "Circle is rolling." << endl; }
};

class Square : public Shape
{
public:
  void draw() const { cout << "Drawing a sqaure." << endl; }
};

int main() {
  Shape* shape1 = new Circle;                               // 不知道 Circle 和 Square 是否实现了 Rollable 的接口,
  Shape* shape2 = new Square;                               // 因此通过 dynamic_cast 进行“试探”:
  if (Rollable* rollable = dynamic_cast<Rollable*>(shape1)) // dynamic_cast 成功,说明 Circle 是 Rollable 的子类,
    rollable->roll();                                       // 可以调用 Rollable 的接口;
  if (Rollable* rollable = dynamic_cast<Rollable*>(shape2)) // dynamic_cast 失败,说明 Square 不是 Rollable 的子类,
    rollable->roll();                                       // 则不调用 Rollable 的接口

  return 0;
}

05. Clear-and-minimize

用途

  • 也叫 swap with temporary
  • 清空一个容器,并将其容量最小化

示例代码

int main() {
  vector<int> int_vec{0, 1, 2, 3, 4, 5, 6, 7};
  int_vec.push_back(100);
  int_vec.push_back(101);
  int_vec.push_back(102);
  int_vec.push_back(103);
  cout << "Capacity of int_vec: " << int_vec.capacity() << endl; // Capacity of int_vec: 16
  int_vec.erase(int_vec.begin() + 5, int_vec.end());
  cout << "Capacity of int_vec: " << int_vec.capacity() << endl; // Capacity of int_vec: 16
  vector<int>().swap(int_vec);  // 首先创建一个空的临时容器,然后将目标容器与空容器交换,
                                // 使得目标容器为空,而原空容器则随机释放
  cout << "Capacity of int_vec: " << int_vec.capacity() << endl; // Capacity of int_vec: 0

  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值