//############################################################################
/* 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的更多相关文章

  1. 常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could not locate named parameter错误的解决

    正确写法: @Override @SuppressWarnings("unchecked") public List<Device> queryOSDevice(Str ...

  2. HQL查询 HQL Named parameter [xxx] not set 的解决办法

    org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter [xxx] not set; nest ...

  3. Nhibernate/Hibernate 使用多参数存儲過程 出現could not execute query,Could not locate named parameter等錯誤解決

    <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns=" ...

  4. 关于C++构造函数的FAQ

    [1] 构造函数是用来干什么的? 构造函数构建类的对象,初始化类变量,分配资源(内存.文件.信号量.套接口等等) [2] List x; 和 List x();有什么不同? 前一个是定义List的一个 ...

  5. wx

    wx The classes in this module are the most commonly used classes for wxPython, which is why they hav ...

  6. Spring Named Parameters examples in SimpleJdbcTemplate

    In JdbcTemplate, SQL parameters are represented by a special placeholder "?" symbol and bi ...

  7. .NET手记-Autofac进阶(传递注册参数 Passing Parameters to Register)

    当你注册组件时,可以为组件服务传入一系列参数,用于服务解析时使用. 可使用的参数类型 Available Parameter Types Autofac提供了集中参数匹配类别: NamedParame ...

  8. ordinal parameter mismatch

    © 版权声明:本文为博主原创文章,转载请注明出处 错误描述:Caused by: org.hibernate.HibernateException: ordinal parameter mismatc ...

  9. ETL利器Kettle

    ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析 ...

随机推荐

  1. javascript的this多种场景用法

    作者:刘志祥 时间:2017.11.10 参考:阮一峰的官方网站 this 是javaScript中的一个关键字,只能在函数内使用.随着场合的不同,this的值会发生变化. 1. 单纯的全局函数调用, ...

  2. js中三种定义变量的方式const, var, let的区别。

    const   var  let区别 1.const 定义的变量不可以修改,而且必须初始化 const a = 3;正确 const a;错误,必须初始化 console.log("函数外c ...

  3. 再回首 基本数据类型和 if语句

    一 变量:(使用变量是不能加引号,要不就变成字符串了) 变量的命名规则: 1.数字,字母,下划线组成. 2.变量不能是数字开头 3.区分大小写 4.不要使用中文或者拼音 5.要有相应的意义 6.不能使 ...

  4. 51Nod 1084:矩阵取数问题 V2(多维DP)

    1084 矩阵取数问题 V2  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...

  5. 软工实践——结对作业2【wordCount进阶需求】

    附录: 队友的博客链接 本次作业的博客链接 同名仓库项目地址 一.具体分工 我负责撰写爬虫爬取信息以及代码整合测试,队友子恒负责写词组词频统计功能的代码. 二.PSP表格 PSP2.1 Persona ...

  6. 2017.4.5 OpenStack简介

    OpenStack 管理的资源不是单机的而是一个分布的系统,把分布的计算.存储.网络.设备.资源组织起来,形成一个完整的云计算系统:OpenStack 也提供一个 UI,这里包括一个图形化的 UI:H ...

  7. mvc core2.1 Identity.EntityFramework Core 用户Claims查看(七)

    添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...

  8. mysql新建用户在本地无法登录

    新建了一个mysql用户,但是无法在本地登录,即使已经授权任一ip都可以登录,甚至特地写清楚localhost登录,还是不行,情况如下 [root@localhost zabbix-release-3 ...

  9. MySQL Profiling--常用命令

    ##=====================================## ## 查看PROFILING是否开启 SELECT @@profiling ## 开始会话级别PROFILING # ...

  10. UML异步怎么表达

    直接看结果 第一虚框和第二虚框是异步的.m1 和m2 没有任何先后关系. 第一虚框和第二虚框是异步的,两者没有任何先后关系.m3和m4是有先后关系的.m3()比m4()先执行.同样m5()和m6().