//############################################################################
/* 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. memoization

    memoization 是指通过缓存函数返回结果来加速函数调用的一种技术.仅当函数是纯函数 时结果才可以被缓存,也就是说,函数不能有任何副作用或输出,也不能依赖任何全局状态 import math _ ...

  2. [LeetCode&Python] Problem 292. Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. 各种浏览器兼容trim()的方法

    一.利用while方法解决 function trim(str) { while (str[0] == ' ') { str = str.slice(1); } while (str[str.leng ...

  4. c++——数据结构

    1.写一个函数PrintN,使得传入一个N,打印从1到N的全部整数 #include<stdio.h> //循环实现 void PrintN(int N){ int i; ;i<=N ...

  5. java-Integer类

    1.为什么会有基本类型包装类 * 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据. 2.常用操作 * 常用的操作之一:用于基本数据类型与字符串之间的转换. * Intege ...

  6. centos6安装opencv2

    环境 centos6.5 opencv2.4 python2.6(centos自带的) 下载 opencv可从GitHub下载https://github.com/opencv/opencv/tree ...

  7. Ubuntu遇到问题“Could not install packages due to an EnvironmentError: [Errno 13] 权限不够: ”

    Ubuntu在使用一些pip的时候会遇到:“Could not install packages due to an EnvironmentError: [Errno 13] 权限不够:”的问题. 在 ...

  8. ADB连接手机的两种方式(usb数据线连接和wifi连接)

    ADB(Android Debug Bridge)安卓测试桥,它是连接电脑开发端和安卓设备的桥梁,这个安卓设备可以是真实的安卓手机或者平板,也可以是虚拟的安卓模拟器,   这里介绍ADB连接手机的两种 ...

  9. 替换元素(replace,replace_if,replace_copy,replace_copy_if)

    replace 审阅range中的每个元素,把old_value替换为new_value template <class ForwardIterator,class T> void rep ...

  10. day 29 socketsetserver 模块

    1.FTP上传/下载服务端/客户端. --------------------------------------------------------------------------------- ...