C++进阶--Named Parameter Idiom
//############################################################################
/* Named Parameter Idiom */
/* 主要解决的问题
C++函数只支持位置参数,不支持像Python那样的命名参数
*/
class OpenFile {
public:
OpenFile(string filename, bool readonly=true, bool appendWhenWriting=false,
int blockSize=256, bool unbuffered=true, bool exclusiveAccess=false);
}
//像下面的函数调用,我得记得每个参数的意思和位置,非常不方便,可读性差,而且不灵活
OpenFile pf = OpenFile("foo.txt", true, false, 1024, true, true);
// 理想的情况是像下面这样的:
OpenFile pf = OpenFile(.filename("foo.txt"), .blockSize(1024) );
/* 解决方法 1 */
class OpenFile {
public:
OpenFile(std::string const& filename);
OpenFile& readonly(bool ro) { readonly_ = ro; return *this; }
OpenFile& createIfNotExist(bool c) { createIfNotExist_ = c; return *this; }
OpenFile& blockSize(unsigned nbytes) { blockSize_ = nbytes; return *this; }
...
};
OpenFile f = OpenFile("foo.txt")
.blockSize(1024)
.createIfNotExist(true)
.appendWhenWriting(true)
.unbuffered(false)
.readonly(true)
.exclusiveAccess(false);
OpenFile f = OpenFile("foo.txt").blockSize(1024);
/* 问题:
* 如果是非成员函数呢?
*/
/* 方法 2: 使用类型*/
void setBirthDate(int month, int day, int year);
setBirthDate(3, 1, 2012); // 1月3日还是3月1日?
//定义结构体类型
struct Day {
explicit Day(int d):val(d){} //设置为explicit,不能隐式转换
int val;
}
struct Month {
explicit Month(int d):val(d){} //设置为explicit,不能隐式转换
int val;
}
struct Year {
explicit Year(int d):val(d){} //设置为explicit,不能隐式转换
int val;
}
void setBirthDate(Month m, Day d, Year y);
setBirthDate(Month(3), Day(1), Year(2010)); //正确
setBirthDate(3, 1, 2010); // 编译不过,很难使用出错
//############################################################################
/* Template Specialization for STL
*
* Specialize the standard library templates' behavior for our class
*
* std:: is a special namespace where we are not allowed to alter its contents
* But we can specialize them for our types
*/
class collar;
class dog {
collar* pCollar;
dog(string name = "Bob") {pCollar = new collar(); cout << name << " is born." << endl; }
}
int main() {
dog dog1("Henry");
dog dog2("Boq");
std::swap(dog1, dog2);
}
C++进阶--Named Parameter Idiom的更多相关文章
- 常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could not locate named parameter错误的解决
正确写法: @Override @SuppressWarnings("unchecked") public List<Device> queryOSDevice(Str ...
- HQL查询 HQL Named parameter [xxx] not set 的解决办法
org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter [xxx] not set; nest ...
- Nhibernate/Hibernate 使用多参数存儲過程 出現could not execute query,Could not locate named parameter等錯誤解決
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns=" ...
- 关于C++构造函数的FAQ
[1] 构造函数是用来干什么的? 构造函数构建类的对象,初始化类变量,分配资源(内存.文件.信号量.套接口等等) [2] List x; 和 List x();有什么不同? 前一个是定义List的一个 ...
- wx
wx The classes in this module are the most commonly used classes for wxPython, which is why they hav ...
- Spring Named Parameters examples in SimpleJdbcTemplate
In JdbcTemplate, SQL parameters are represented by a special placeholder "?" symbol and bi ...
- .NET手记-Autofac进阶(传递注册参数 Passing Parameters to Register)
当你注册组件时,可以为组件服务传入一系列参数,用于服务解析时使用. 可使用的参数类型 Available Parameter Types Autofac提供了集中参数匹配类别: NamedParame ...
- ordinal parameter mismatch
© 版权声明:本文为博主原创文章,转载请注明出处 错误描述:Caused by: org.hibernate.HibernateException: ordinal parameter mismatc ...
- ETL利器Kettle
ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析 ...
随机推荐
- 阿里云ECS服务器购买流程 (自定义配置购买、按月、按量购买)教程
阿里ECS云服务器自定义购买流程 本文提供全图文流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- CodeForces - 1099F:Cookies (线段树)
Mitya and Vasya are playing an interesting game. They have a rooted tree with n vertices, and the ve ...
- SVD分解求解旋转矩阵
1.设是两组Rd空间的点集,可根据这两个点集计算它们之间的旋转平移信息. 2.设R为不变量,对T求导得: 令 则 将(4)带入(1)得: 令 则 (相当于对原来点集做减中心点预处理,再求旋转量) 3. ...
- AE1
i2.1 导入四种方式: 1.文件->导入 2.项目窗口空白区域双击 3.空白区域右键 4 ctrl+i 常用键: ctri+i ctrl shift alt delete 2.2移动属 ...
- jeecg-org.jeecgframework.web.system.listener.InitListener
早上启动项目 发现报错 百度之后,发现这属于jeecg常见问题: http://www.jeecg.org/forum.php?mod=viewthread&tid=1830&extr ...
- 快速排序 C语言实现
转载于> http://blog.chinaunix.net/uid-26404477-id-3329885.html 总的关键字比较次数:O(nlgn) 尽管快速排序的最坏时间为O(n2),但 ...
- 基于Flask开发web微信
1. 获取二维码 app.py import re import time import requests from flask import Flask,render_template app = ...
- 20165313 预备作业3 Linux安装及学习
虚拟机安装 刚开始我觉得既然有了教程,安装虚拟机应该是很简单的事情,然而由于电脑本身系统地地问题,导致我数次安装失败,后来咨询了老师并查阅了资料,最终才安装好. 其中最主要的问题就是电脑虚拟化的修改. ...
- Redis(二)持久化
Redis持久化,分为RDB方式和AOF方式,它们可以单独使用,也可以混用.Redis默认的是使用RDB方式. 一.RDB方式 1.触发快照的方式 RDB方式是在指定时间间隔内某一时间点的数据集快照. ...
- CH5701 开车旅行
题意 5701 开车旅行 0x50「动态规划」例题 描述 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...