Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ad2484

Browse files
author
dev0
committed
[hcheng] chapter 1.4.12 Default and Value initialization
1 parent 14854f6 commit 9ad2484

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 1.4.12 Default and Value Initialization
2+
#include<string>
3+
class A {
4+
public:
5+
std::string s_;
6+
int i_;
7+
A(){} // this will default-construct s_ but leave i_ uninitialized
8+
// ch: in c++11, still be this?
9+
};
10+
11+
/*Good as following*/
12+
/*
13+
* if T is as class with a default constructor, that will be used
14+
* otherwise, the storage for T will be set to 0
15+
* if T is an array, each element will be recursively initialized
16+
*/
17+
template<typename T>
18+
struct initialized_value {
19+
T result;
20+
initialized_value() : result() {
21+
}
22+
};
23+
24+

meta_program/chapter_1_specializtion_and_deduction/main.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
//#include "specialization_and_overload.h"
33
//#include "specialization_in_class.h"
44
#include "tempalte_argument_deduction.h"
5+
#include "default_and_value_initialization.h"
56
using namespace std;
67

78
int main(int argc, char** argv) {
9+
A a;
10+
cout << "a.i_=" << a.i_ << "\n";
11+
initialized_value<double> x;
12+
cout << "x.result=" << x.result << "\n";
13+
initialized_value<double [5]> y;
14+
cout << "y.result= ";
15+
for (int i = 0;i < 5;++i) {
16+
cout << y.result[i] << " ";
17+
}
18+
cout << "\n";
19+
initialized_value<std::string> z;
20+
cout << "z.result = " << z.result << "\n";
821
return 0;
922
}
1023

0 commit comments

Comments
 (0)