-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cc
More file actions
48 lines (39 loc) · 943 Bytes
/
demo.cc
File metadata and controls
48 lines (39 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstring>
#include <iostream>
#include <string>
struct Point {
int x;
int y;
std::string name;
};
class Base {
public:
Base(int n) { data_ = n; }
explicit Base(int x, int y) { data_ = x + y; }
virtual bool Equal(Base* other = nullptr) = 0;
private:
int data_;
};
class Derived : public Base {
public:
bool EqualDynamic(Base* other);
bool EqualStatic(Base* other);
private:
int data_;
};
bool Derived::EqualDynamic(Base* other) {
Derived* that = dynamic_cast<Derived*>(other);
return this->data_ == that->data_;
}
bool Derived::EqualStatic(Base* other) {
Derived* that = static_cast<Derived*>(other);
return this->data_ == that->data_;
}
int main() {
Point p1, p2;
memset(&p1, 0, sizeof(p1)); // sizeof(varname)
memset(&p2, 0, sizeof(Point)); // sizeof(type)
float f = 3.5;
int c_cast = (int)f;
int cpp_cast = static_cast<int>(f);
}