The difference between the copy-to-backup structure and the value = is that the copy-to-backup structure is performed on the uninitialized memory. = Is an operation performed on the initialized memory.
The following class converts data of the int and char * types to Char []. The converted characters are left aligned, and spaces are filled in the white space on the right.
1 template <int size>
2 struct Str
3 {
4 public:
5 char value [size];
6 public:
7 STR (const char * m)
8 {
9 memset (this, 0x20, sizeof (* This ));
10 // Buffer Overflow check
11 if (strlen (m)> = size)
12 {
13 throw ("too long arguement ");
14}
15 strncpy (this-> value, M, size );
16 this-> value [strlen (m)] = 0x20;
17}
18 STR ()
19 {
20 memset (this, 0x50, sizeof (* This ));
21}
22 ~ STR ()
23 {
24
25}
26 STR & operator = (const char * m)
27 {
28 // Buffer Overflow check
29 If (strlen (m)> = size)
30 {
31 throw ("too long arguement ");
32}
33 strncpy (this-> value, M, size );
34 // pad with write Space
35 memset (& this-> value [strlen (m)], 0x20, size-strlen (m ));
36 return * this;
37
38}
39
40 STR & operator = (INT m)
41 {
42 assert (size> = 10 );
43 snprintf (this-> value, size, "% d", M );
44 This-> value [strlen (this-> value)] = 0x20;
45 return * this;
46}
47 STR (INT m)
48 {
49 memset (this, 0x20, sizeof (* This ));
50 assert (size> = 10 );
51 snprintf (this-> value, size, "% d", M );
52 This-> value [strlen (this-> value)] = 0x20;
53}
54
55 };
56
The following class uses the above Code:
1 class swapdata
2 {
3 public:
4 swapdata ()
5 {
6 memset (this, 0x20, sizeof (* This ));
7}
8 ~ Swapdata ()
9 {
10
11}
12 public:
13 STR <12> m_nbillnum;
14 STR <12> m_litemleft;
15 STR <10> m_litemtop;
16 };
Test code: