-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
291 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Type Cast | ||
|
||
!!! info | ||
- **隐式类型转换** | ||
- **显式类型转换** | ||
- **整型提升** | ||
- **无符号类型转换** | ||
![Alt text](https://zzh-pic-for-self.oss-cn-hangzhou.aliyuncs.com/img/202404261107274.png) | ||
|
||
## 强制类型转换 | ||
|
||
`cast-name<type>(expression)` | ||
|
||
### static_cast | ||
|
||
> 只要不包含底层的const就可以使用 | ||
- `static_cast` 在相关的类型之间转换,比如 | ||
- 一个父类和派生类之间的转换 | ||
- `void * `到其他指针类型之间的转换 | ||
- `int` `float`等类型之间的转换 | ||
- 它的安全性比较高, | ||
|
||
```CPP | ||
double m; | ||
void *p = &m; | ||
double *dp = static_cast<double *>(p); | ||
``` | ||
|
||
### const_cast | ||
|
||
> 只能改变底层const | ||
> | ||
|
||
|
||
### reinterpret_cast | ||
|
||
> 为转换对象的位模式提供更低层次的重新解释 | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Overload | ||
|
||
!!! info "info" | ||
- | ||
## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class A | ||
{ | ||
public: | ||
A(int i) : mi(i) {} | ||
A(const A &rhs) : mi(rhs.mi) | ||
{ | ||
cout << "A::A(&)" << endl; | ||
} | ||
A &operator=(const A &rhs) | ||
{ | ||
mi = rhs.mi; | ||
cout << "A::operator=()" << endl; | ||
return *this; | ||
} | ||
virtual void f() | ||
{ | ||
cout << "virtual A::f(), " << mi << endl; | ||
} | ||
void g() | ||
{ | ||
cout << "A::g()" << mi << endl; | ||
} | ||
|
||
protected: | ||
int mi; | ||
}; | ||
|
||
class B : public A | ||
{ | ||
public: | ||
B(int i, int j) : A(i), mj(j) {} | ||
void f() override | ||
{ | ||
cout << "B::f(), " << mi << ", " << mj << endl; | ||
} | ||
void B_single() | ||
{ | ||
cout << "B::B_single()" << mi << "," << mj << endl; | ||
} | ||
void g() | ||
{ | ||
cout << "B::g()" << mi << "," << endl; | ||
} | ||
|
||
private: | ||
int mj; | ||
}; | ||
|
||
class Base | ||
{ | ||
private: | ||
public: | ||
int a; | ||
const int consA; | ||
Base(int a) : Base(a, a) | ||
{ | ||
cout << "Call Base(int a)" << endl; | ||
} | ||
Base(int m, int ConstM) : a(m), consA(ConstM) | ||
{ | ||
cout << "Call Base(int m, int ConstM)" << endl; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
|
||
A *a = new A(1); | ||
B *b = new B(2, 5); | ||
a->f(); | ||
b->f(); | ||
/* | ||
A::f(), 1 | ||
B::f(), 2, 5 | ||
*/ | ||
// a(1) | ||
// 转换之后,调用, 虚函数仍然是原来的,这与vitual Table相关 | ||
(static_cast<B *>(a))->f(); | ||
(static_cast<B *>(a))->B_single(); | ||
(static_cast<B *>(a))->g(); | ||
/* | ||
>> A::f(), 1 | ||
>> B::B_single()1,0 | ||
>> B::g()1 | ||
*/ | ||
// b(2,5) | ||
(static_cast<A *>(b))->f(); | ||
// >> B::f(), 2, 5 | ||
(static_cast<A *>(b))->g(); | ||
// >> A::g() 2 | ||
// (static_cast<A *>(b))->B_single(); | ||
} |