c++ telescoping constructor is NOT supported until c++11

Telescoping constructor: see Effective Java 2nd Edition Item 2

If you want to use telescoping constructor but your compiler does not support c++11 standards, you can use default parameter instead, see code snippets ‘main.cpp‘, ‘Box.h‘, ‘Box.cpp‘.

If your compiler supports c++11 standards, the syntax of telescoping is shown in code snippet ‘Box2.h‘ and ‘Box2.cpp‘, and you might need to use ‘-std=c++11‘ parameter when you are compiling your source code.

For now, I‘d like to use default parameter.

main.cpp

 1 #include "Box.h"
 2
 3 int main()
 4 {
 5     Box box;
 6     box.print();
 7     Box box2(1);
 8     box2.print();
 9     Box box3(1, ‘A‘);
10     box3.print();
11     Box box4(1, ‘A‘, 1.88);
12     box4.print();
13     return 0;
14 }

Box.h

 1 #ifndef BOX_H
 2 #define BOX_H
 3
 4 class Box
 5 {
 6     public:
 7         Box(const int &_a = 0, const char &_b = ‘M‘, const double &_c = 0.99);
 8         ~Box();
 9         void print();
10
11     private:
12         int a;
13         char b;
14         double c;
15 };
16
17 #endif // BOX_H

Box.cpp

 1 #include "Box.h"
 2
 3 #include <iostream>
 4
 5 using namespace std;
 6
 7 Box::Box(const int &_a, const char &_b, const double &_c)
 8 : a(_a), b(_b), c(_c)
 9 {
10
11 }
12
13 Box::~Box()
14 {
15
16 }
17
18 void Box::print() {
19     cout << "a = " << a << ",b = " << b << ",c = " << c << endl;
20 }

Box2.h

 1 #ifndef BOX_H
 2 #define BOX_H
 3
 4 class Box
 5 {
 6     public:
 7         Box(const int &_a, const char &_b, const double &_c);
 8         Box(const int &_a, const char &_b);
 9         Box(const int &_a);
10         Box();
11         ~Box();
12         void print();
13
14     private:
15         int a;
16         char b;
17         double c;
18 };
19
20 #endif // BOX_H

Box2.cpp

 1 #include "Box.h"
 2
 3 #include <iostream>
 4
 5 using namespace std;
 6
 7 Box::Box(const int &_a, const char &_b, const double &_c)
 8 : a(_a), b(_b), c(_c)
 9 {
10
11 }
12
13 Box::Box(const int &_a, const char &_b)
14 : Box(_a, _b, 0.99)
15 {
16
17 }
18
19 Box::Box(const int &_a)
20 : Box(_a, ‘M‘)
21 {
22
23 }
24
25 Box::Box()
26 : Box(0)
27 {
28
29 }
30
31 Box::~Box()
32 {
33
34 }
35
36 void Box::print() {
37     cout << "a = " << a << ",b = " << b << ",c = " << c << endl;
38 }
时间: 2024-09-29 01:52:44

c++ telescoping constructor is NOT supported until c++11的相关文章

Effective Java 英文 第二版 读书笔记 Item 2:Consider a builder when faced with many constructor parameters.

package builderManyPara; //JavaBeans Pattern - allows inconsistency,mandates mutability public class NutritionFactsBean { // Parameters initialized to default values(if any) // required private int servingSize = -1; private int servings = -1; // opti

黑马程序员——Java基础---反射Class类、Constructor类、Field类

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- 反射的应用场景 一.概述 反射技术: Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类中的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的

设计模式:建造模式

原文地址:http://leihuang.org/2014/12/03/builder/ Creational 模式 物件的产生需要消耗系统资源,所以如何有效率的产生.管理 与操作物件,一直都是值得讨论的课题, Creational 模式即与物件的建立相关,在这个分类下的模式给出了一些指导原则及设计的方向.下面列举到的全属于Creational 模式 Simple Factory 模式 Abstract Factory 模式 Builder 模式 Factory Method 模式 Protot

HiveQL逻辑执行顺序

FROM->WHERE->GROUP BY->HAVING->SELECT->ORDER BY Hive总是按照从左到右的顺序执行的,如a.b.c三个表关联 select a.id,b.colname,c.colname from a join b on a.id = b.id join c on a.id = c.id 大多数情况下,hive会对每个join连接对象启动一个MapReduce任务,上面的列子首先会启动一个MapReduce job对表a和表b进行连接操作,然

Effective Java学习笔记

创建和销毁对象 第一条:考虑用静态工厂方法替代构造器 For example: public static Boolean valueOf(boolean b){ return b ? Boolean.TRUE : Boolean.FALSE; } 优势: 有名称 不必在每次调用它们的时候都创建一个新对象 它们可以返回原返回类型的任何子类型的对象 在创建参数化类型实例的时候,他们使代码变得更加简洁 缺点: 类如果不含公有的或者受保护的构造器,就不能被子类化 它们与其他的静态方法实际上没有任何区别

Item2: 遇到多个构造器形参时考虑使用构建器

Consider a builder when faced with many constructor parameters 引言 遇到多个构造器时要考虑用构建器(builder) 重叠构造器(telescoping constructor) // Telescoping constructor pattern - does not scale well! - Pages 11-12 /** * 营养成分表 */ public class NutritionFacts { private fin

STL vector的构造函数和析构函数(2)

原文来自:点击打开链接 译文: public member function vector的构造器:这里我只翻译C++11的,C++98的就不翻译了. 构造器原型: <vector> std::vector::vector C++98 C++11 default (1) explicit vector (const allocator_type& alloc = allocator_type()); fill (2) explicit vector (size_type n); vec

Java高效编程(2) -- Creating and Destroying Objects

Item 1: Consider static factory methods instead of constructors Advantage: One advantage of static factory methods is that, unlike constructors, they have names. A second advantage of static factory methods is that, unlike constructors, they are not

Builder Pattern

遇到多个构造器形参时要考虑用构建器 引言 遇到多个构造器时要考虑用构建器(builder) 重叠构造器(telescoping constructor) // Telescoping constructor pattern - does not scale well! - Pages 11-12 /** * 营养成分表 */ public class NutritionFacts { private final int servingSize; // (mL) required private