//############################################################################
/* 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. 牛客OI赛制测试赛2

    A题: https://www.nowcoder.com/acm/contest/185/A 链接:https://www.nowcoder.com/acm/contest/185/A来源:牛客网 题 ...

  2. iproute2 与 net-tools

    https://linux.cn/article-4326-1.html https://blog.csdn.net/astrotycoon/article/details/52317288 如今很多 ...

  3. php 函数集锦

    1.array_intersect_assoc()用于比较两个(或更多个)数组的键名和键值,并返回交集. <?php $a1=array("a"=>"red& ...

  4. django ----CBV中加装饰器

    CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...

  5. 原生的js轮播图

    图片会照常循环播放,当然也可以通过按钮来进行切换,当切出当前的页面时,等到你在回到当前页面时该轮播的图片还是停留在你之前所切出去的的那张图片的状态. HTML部分: <html> < ...

  6. hdu3068 最长回文 manacher

    给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 manacher裸题 #include<stdio. ...

  7. 学习笔记TF013:卷积、跨度、边界填充、卷积核

    卷积运算,两个输入张量(输入数据和卷积核)进行卷积,输出代表来自每个输入的信息张量.tf.nn.conv2d完成卷积运算.卷积核(kernel),权值.滤波器.卷积矩阵或模版,filter.权值训练习 ...

  8. execve函数的介绍与使用

    #include<stdio.h> #include<unistd.h> int main() { char *filename[]={"./BP",NUL ...

  9. Cassandra Demo--Python操作cassandra

    ================================================================ 创建keyspace和table CREATE KEYSPACE ex ...

  10. What's New In Zeebe: Scaling Zeebe, New Client APIs, Faster Requests, Timestamps, NodeJS Client, and Default Topic is Back!

    Written by Daniel Meyer on May 16 2018 in the What's New In Zeebe category. Welcome to the first-eve ...