- const_cast,去掉(指针或引用)常量属性的一个类型转换,但需要保持转换前后类型一致
- static_cast,提供编译器认为安全的类型转换(最常使用)
- reinterpret_cast,类似于c语言风格的强制类型转换,不保证安全;
- dynamic_cast,主要用于继承结构中,可以支持RTTI类型识别的(基类到派生类)转换
const_cast 1 2 3 4 5 6 7 8 9 10 11 12 const int a = 10 ;int * p = const_cast <int *>(&a);int ref = const_cast <int &>(a);int b = const_cast <int > (a);char * p = const_cast <char *>(&a);
static_cast 1 2 3 4 5 6 7 8 9 int b = 10 ;char c = static_cast <char > (b);int *p = nullptr ;short * p1 = static_cast <short > (p);
这里可以转换编译器认为有联系的类型,拒绝不安全的类型转换。
reinterpret_cast转换 1 2 3 4 int * p = nullptr ;double * p1 = reinterpret_cast <double *> (p);
dynamic_cast转换 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 class Animal {public : virtual void bark () = 0 ; }; class Dog : public Animal {public : void bark () { cout << "Dog Woof! Woof!" << endl; } }; class Cat : public Animal {public : void bark () { cout << "Cat Meow! Meow!" << endl; } }; void showAnimal (Animal* animal) { animal->bark (); } int main () { Dog dog; Cat cat; showAnimal (&dog); showAnimal (&cat); return 0 ; }
这里通过的showAnimal函数通过基类指针指向不同的派生类,从而实现调用其对应的重写方法。 但是比如说,对于Dog类,我想调用实现其另一个eat方法。
1 2 3 4 5 6 class Dog : public Animal {public : void bark () { cout << "Dog Woof! Woof!" << endl; } void eat () { cout << "Dog like eat bone!" <<endl; } };
当调用showAnimal方法时,如果其指向的时Dog方法,就调用eat方法,就可以通过dynamic_cast实现,如代码所示
1 2 3 4 5 6 7 8 9 10 11 void showAnimal (Animal* animal) { Dog* dog = static_cast <Dog*>(animal); if (dog) { dog->eat (); } else { animal->bark (); } }
dynamic_cast<Dog*>(animal) 会检查 animal 是否真的指向 Dog 对象:
如果是,返回 Dog*;
如果不是,返回 nullptr(而不是崩溃或未定义行为)
我们之前也说了static_cast也可以进行基类和派生类的转换,但是这里是不会判断直接进行转换的,不管基类指针是不是指向Dog的派生类,都一律被转换成Dog的派生类;而dynamic_cast会进行判断,只有基类指针指向的是Dog类型的派生类时,才发生转换,否则返回nullptr。
最后,目前就先总结这些,后续项目中遇到了再添加内容。
$share-item-width = 1.8rem
.post-share-container {
flex-shrink 0
.share-list-wrap {
display flex
justify-content flex-end
.share-item {
width $share-item-width
height $share-item-width
margin-left 0.5rem
padding 0.4rem
border-style solid
border-width 0.1rem
border-radius 50%
cursor pointer
transition-t("background", "0", "0.3", "ease")
i {
color inherit
font-size 1rem
}
&.qq {
color var(--keep-primary-color)
border-color var(--keep-primary-color)
&:hover {
color var(--background-color-1)
background var(--keep-primary-color)
}
}
&.wechat {
color var(--keep-success-color)
border-color var(--keep-success-color)
img {
filter brightness(1) !important
&[lazyload] {
&::before {
background #fff !important
}
}
}
&:hover {
color var(--background-color-1)
background var(--keep-success-color)
}
}
&.weibo {
color var(--keep-danger-color)
border-color var(--keep-danger-color)
&:hover {
color var(--background-color-1)
background var(--keep-danger-color)
}
}
}
}
}
if (hexo-config('comment') && hexo-config('comment.enable') == true && hexo-config('comment.use')) {
if (hexo-config('comment.use') == "valine") {
@import "./valine.styl"
}
else if (hexo-config('comment.use') == "gitalk") {
@import "./gitalk.styl"
}
else if (hexo-config('comment.use') == "twikoo") {
@import "./twikoo.styl"
}
else if (hexo-config('comment.use') == "waline") {
@import "./waline.styl"
}
}
.comments-container {
display inline-block
width 100%
margin-top var(--component-gap)
.comment-area-title {
width 100%
color var(--text-color-3)
font-size 1.38rem
line-height 2
i {
color var(--text-color-3)
}
+keep-tablet() {
font-size 1.2rem
}
}
.configuration-items-error-tip {
display flex
align-items center
margin-top 1rem
color var(--text-color-3)
font-size 1rem
i {
margin-right 0.3rem
color var(--text-color-3)
font-size 1.2rem
}
}
.comment-plugin-fail {
display none
flex-direction column
align-items center
justify-content space-around
width 100%
padding 2rem
.fail-tip {
color var(--text-color-3)
font-size 1.1rem
}
.reload {
margin-top 1rem
}
}
.comment-plugin-loading {
flex-direction column
padding 1rem
color var(--text-color-3)
.loading-icon {
color var(--text-color-4)
font-size 2rem
}
.load-tip {
margin-top 1rem
color var(--text-color-4)
font-size 1.1rem
}
}
}