Skip to content

Commit 3d2430a

Browse files
committed
update chapter 15
1 parent 0b3addf commit 3d2430a

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

.gitignore

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
*.out
22
settings.json
3-
week01/~$Lecture01.pptx
4-
week02/~$Lecture02.pptx
5-
week03/~$Lecture03.pptx
3+
.DS_Store

week15/Lecture15.pptx

-1.52 KB
Binary file not shown.

week15/examples/const_cast.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
#include <iostream>
22
using namespace std;
33

4-
void increase(const int * pn)
5-
{
6-
int * pi;
7-
pi = const_cast<int *>(pn);
8-
(*pi)++; // undefined behavior
9-
}
10-
11-
124
int main()
135
{
146
int value1 = 100;
157
const int value2 = 200;
168
cout << "value1 = " << value1 << endl;
179
cout << "value2 = " << value2 << endl;
18-
10+
1911
int * pv1 = const_cast<int *>(&value1);
2012
int * pv2 = const_cast<int *>(&value2);
2113

@@ -25,12 +17,14 @@ int main()
2517
cout << "value1 = " << value1 << endl;
2618
cout << "value2 = " << value2 << endl;
2719

28-
int v2 = const_cast<int&>(value2);
20+
int& v2 = const_cast<int&>(value2);
2921
v2++;
3022
cout << "value2 = " << value2 << endl;
3123

32-
// cout << "*pv2 = " << (*pv2) << endl;
33-
// cout << "v2 = " << v2 << endl;
24+
cout << "*pv2 = " << (*pv2) << endl;
25+
cout << "v2 = " << v2 << endl;
3426

35-
return 0;
27+
return 0;
3628
}
29+
30+

week15/examples/nested-enum1.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Mat
2020

2121
int main()
2222
{
23-
Mat image(DataType::TYPE8U);
23+
Mat image(TYPE8U);
2424

25-
if (image.getType() == DataType::TYPE8U)
25+
if (image.getType() == TYPE8U)
2626
std::cout << "This is an 8U matrix." << std::endl;
2727
else
2828
std::cout << "I am not an 8U matrix." << std::endl;

week15/examples/reinterpret_cast.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int i = 18;
7+
float * p1 = reinterpret_cast<float *>(i); // static_cast will fail
8+
int * p2 = reinterpret_cast<int*>(p1);
9+
10+
printf("p1=%p\n", p1);
11+
printf("p2=%p\n", p2);
12+
13+
return 0;
14+
}

week15/examples/rtti.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ int main()
2929
cout << person.getInfo() << endl;
3030
cout << ps->getInfo() << endl; // danger if getInfo is not virtual
3131

32-
// ps = dynamic_cast<Student*>(&person);
33-
// printf("address = %p\n", ps);
34-
// pp = dynamic_cast<Person*>(&student);
35-
// printf("address = %p\n", pp);
32+
ps = dynamic_cast<Student*>(&person);
33+
printf("address = %p\n", ps);
34+
pp = dynamic_cast<Person*>(&student);
35+
printf("address = %p\n", pp);
3636
return 0;
3737
}

0 commit comments

Comments
 (0)