Skip to content

Commit b269ec9

Browse files
author
lulongfei
committed
feat: update cpp files
1 parent 4d02c59 commit b269ec9

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

explicit.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
3+
class A
4+
{
5+
6+
public:
7+
A() = default;
8+
explicit A(int t_height)
9+
: height(t_height)
10+
{}
11+
12+
void GetHeight()
13+
{
14+
std::cout << height << std::endl;
15+
}
16+
17+
private:
18+
int height;
19+
20+
};
21+
22+
23+
int main()
24+
{
25+
26+
// A c = 95; // error, 因为A的构造函数加了explicit,只能是显示地转换,不能是隐式转换
27+
A c = (A)98; // corrent,只能显示地转换
28+
c.GetHeight();
29+
return 0;
30+
}

mem_model.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
3+
typedef struct Point3
4+
{
5+
float x;
6+
float y;
7+
float z;
8+
} BigPoint3;
9+
10+
11+
class Point3d
12+
{
13+
public:
14+
Point3d(float x= 0.0, float y = 0.0, float z= 0.0)
15+
:_x(x),_y(y),_z(z){}
16+
17+
float x() const {return _x;}
18+
float y() const {return _y;}
19+
float z() const {return _z;}
20+
21+
private:
22+
float _x;
23+
float _y;
24+
float _z;
25+
};
26+
27+
28+
inline std::ostream& operator<<(std::ostream& os, const Point3d& pt)
29+
{
30+
os << "Point3d(" << pt.x() << ", " << pt.y() << ", " << pt.z() << ")";
31+
return os;
32+
}
33+
34+
class Base
35+
{
36+
public:
37+
Base(int i): baseI(i){};
38+
virtual void print(void){std::cout << "调用了虚函数Base::print()";}
39+
virtual void setI(){std::cout << "调用了虚函数Base::setI";}
40+
virtual ~Base(){}
41+
public:
42+
int baseI;
43+
};
44+
45+
46+
int main()
47+
{
48+
// BigPoint3 a = {1,2,3};
49+
// std::cout << (&a) << std::endl;
50+
// std::cout << &(a.x) << std::endl;
51+
// std::cout << *((float *)(&a)) << std::endl;
52+
// std::cout << *((float *)(&a)+1) << std::endl;
53+
// std::cout << *((float *)(&a)+2) << std::endl;
54+
55+
// Point3d b{2, 4, 9};
56+
// std::cout << b << std::endl;
57+
typedef void(* Func)(void);
58+
59+
Base b(1000);
60+
std::cout << ((int *)(&b)) << std::endl;
61+
std::cout << ((int *)(&b.baseI)) << std::endl;
62+
std::cout << (int *)*(int *)(&b) << std::endl;
63+
Func vfunc = (Func)*((int *)*(int *)(&b));
64+
vfunc();
65+
66+
// getchar();
67+
}

0 commit comments

Comments
 (0)