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实战应用解析 ...
随机推荐
- maven 构建 war文件&&Glassfish运行+部署war文件+访问(命令行模式)
Glassfish常用命令 asadmin start-domain --verbose #启动Glassfish服务器(默认domain1) ,并在终端显示相关信 ...
- arcgis server备份还原过程
一.备份过程 1.找到已经安装的arcgis server安装目录,并找到备份工具: 2.快捷键win + R启动cmd,将备份工具文件拖入cmd窗口,enter 3. 通过backup.py脚本进行 ...
- es6的let与es5的var定义变量的区别
es6的let与es5的var定义变量的区别 自身新手第一次接触let关键字的时候,不知道let与var的区别,本能认为是一样,但非如此,比如下述的代码运行就会报错: let hello = 'hel ...
- innerHTML与innerText功能的强大
例: <div id="study"> <span style="color:red">学习</span>study < ...
- Python算法——递归思想
编程语言在构建程序时的基本操作有:内置数据类型操作.选择.循环.函数调用等,递归实际属于函数调用的一种特殊情况(函数调用自身),其数学基础是数学归纳法.递归在计算机程序设计中非常重要,是许多高级算法实 ...
- EasyUI datagrid combox onchange 五
$("#sTwo").combobox({ onChange: function (n,o) { n改变后,o改变前 }
- lvm入门
实例: 使用lvm存储结构的主机需要扩容,现在我们已经将一个新的硬盘安装上去,将该新的硬盘的空间全部增加到主机上 20 ls /dev/sd* #查看新增加的硬盘名,我的为xvdb 21 ls /de ...
- linux系统调用的三种方法
通过glibc提供的库函数 [23:02:14] gcc chmodtest.c [23:02:17] ls -l kali //记得先创建这个文件 -rwxrwxrwx. 1 root root 0 ...
- Go语言实战 (William,Kennedy 等著)
第1章 关于Go语言的介绍 (已看) 1.1 用Go解决现代编程难题 1.1.1 开发速度 1.1.2 并发 1. goroutine 2. 通道 1.1.3 Go语言的类型系统 1. 类型简单 2. ...
- Unity 官方文档学习
Manual Texture Components:https://docs.unity3d.com/Manual/comp-Textures.html Profiler:https://docs.u ...