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实战应用解析 ...
 
随机推荐
- 论Injection的前世今生
			
Click me~ why Java EE provides injection mechanisms that enable your objects to obtain references to ...
 - ROI-Align解决方案
			
https://yq.aliyun.com/articles/558181 Mask R-CNN与Faster R-CNN相似,Faster R-CNN是two-stage的,其中第一个stage是R ...
 - Mypwd 的解读与实现 20155208
			
Mypwd 的解读与实现 20155208 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...
 - 20155208徐子涵Vim编辑器学习经验
			
20155208徐子涵 2016-2017-2 Vim编辑器学习经验 当我们运用虚拟机进行书写代码时,我们就会用到Vim编辑器,用Vim编辑器进行编辑特别方便,而Vim编辑器中也有一些操作需要去学习. ...
 - 51Nod 1007:正整数分组(01背包)
			
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 ...
 - Learning by doing——小黄杉获得感想
			
突然想起来前一个月答应了栋哥要写一篇博客的,后来一直忙于复习就忘了. 不过答应了的事就要完成嘛. 获得感言 首先就是非常高兴的了,这也是对我的能力的一种肯定 这次的获得原因是期中考最快满分,emmm侧 ...
 - 【次小生成树】【Kruskal】【prim】【转】
			
原博客出处:https://blog.csdn.net/yasola/article/details/74276255 通常次小生成树是使用Prim算法进行实现的,因为可以在Prim算法松弛的同时求得 ...
 - 调试 shell script 方法
			
wade@V1088:~$ cat b.sh#!/bin/bash dir=`pwd` dir=$dir'/' for f in `ls *.png` do echo $dir$f done 看每一行 ...
 - 用idea编写第一个jsp文件
			
创建一个JAVA-web项目的前提:1.下载并安装JDK2.安装并配置Tomcat服务器 下面开始创建JAVA-web项目: 1.File——>new——>Project... 2.跟 ...
 - OutputStream 和 Writer
			
OutputStream类(直接操作byte数组) 该类是字节输出流的抽象类,定义了输出流的各种操作方法.如下图是OutputStream的层次结构: ByteArrayOutputStream:字节 ...