C/C++ 类型转换机制(四)静态转换

static_cast 本质是:执行 C++ 标准明确定义好的转换规则。比如:int 和 double,编译器在编译阶段就知道该怎么转,这时就可以使用 static_cast<double>(int) 进行转换。

反过来,如果两个类型之间根本没有转换规则,比如 int* 和 char*,编译器无法按规则转换,这时只能用 reinterpret_cast 强行按位重新解释。

它是编译期完成的转换,不会在运行期做类型校验。

简言之:static_cast 负责 编译期已有明确规则的转换。转换是否安全,由程序员自己保证。

1. 基础转换

static_cast 首先继承了 C 语言里就有的两类转换:数值类型之间的转换,以及 void* 与其他类型指针之间的转换。

注意:整型、浮点型、bool 之间的相互转换,属于 C++ 标准隐式转换,写不写 static_cast 都能编译。显式写出的好处是让转换意图清晰,明确告诉读者 “这里确实需要类型转换”。

#include <iostream>
using namespace std;


// 1. 数值转换
void test01()
{
    int v1 = 100;
    // 下面的 static_cast 可以省略
    double v2 = static_cast<double>(v1);
    short v3 = static_cast<short>(v1);
    bool v4 = static_cast<bool>(v1);
}

// 2. void* 和其他类型指针转换
// 也可以使用 reinterpret_cast 转换,但是 static_cast 语义更加合适
void test02()
{
    int* ip1 = nullptr;
    void* vp1 = ip1;

    // C++ 标准规定,void* 转换到其他类型指针必须显式转换
    void* vp2 = nullptr;
    int* ip2 = static_cast<int*>(vp2);
}

int main()
{
    test01();
    //test02();

    return 0;
}

它还支持 C++ 特有的转换:枚举与整数互转、左值转右值引用、以及增加 const 限定。

#include <iostream>
using namespace std;


// 1. 枚举类型和整数转换
// enum class 不能隐式地与整数互转,但可以通过 static_cast 显式互转。
enum class Color { Red, Green, Blue };
void test01()
{
    Color c = Color::Red;
    int v = static_cast<int>(c);
    c = static_cast<Color>(v);

    // 注意:v 的取值不在枚举取值范围内,转换在语法上也合法
    v = 100;
    c = static_cast<Color>(v);
}


// 2. 左值和右值引用转换
// static_cast<T&&>(左值) 可以把一个左值转成右值引用。
// 它常用来配合函数重载:让左值走左值引用版本,把转出来的右值交给右值引用版本。
// 日常开发中直接写 std::move 更清晰。
void func(int&&) { std::cout << "右值版本\n"; }
void func(int&) { std::cout << "左值版本\n"; }

void test02()
{
    int lvalue = 100;
    // 适配左值版本
    func(lvalue);
    // 适配右值版本
    func(static_cast<int&&>(lvalue));
    // 多数用下面的写法,更简单
    func(std::move(lvalue));
}


// 3. 特殊用法:增加 const 限定
// 默认来讲,将非 const 指针引用转换为 const,不需要显式转换
// 如果希望显式书写的话,增加 const 建议用 const,去掉 const 用 const_cast
void test03()
{
    int* ip = nullptr;
    // 下面可以直接写
    const int* cp1 = ip;
    // 也可以显式写成下面的形式
    const int* cp2 = static_cast<const int*>(ip);
}

int main()
{
    test01();
    test02();
    test03();

    return 0;
}

2. 继承转换

在类继承体系中,static_cast 支持基类与派生类之间的指针/引用转换,分为向上转换(派生 → 基类)和向下转换(基类 → 派生)两个方向,二者安全性完全不同。

#include <iostream>
using namespace std;


struct A { int x; };
struct B { int y; };
struct C { int z; };
struct D : A, B, C { int w; };


// 1. 向上类型转换
void test01()
{
    D* d = new D;
    A* a = static_cast<A*>(d);
    B* b = static_cast<B*>(d);
    C* c = static_cast<C*>(d);

    // 注意:具有继承关系的指针和引用在类型转换后需要指向目标类型结构,这可能会导致导致指针地址重新计算
    // 因为需要保证转换后能够正确访问
    std::cout << a << " " << b << " " << c << " " << d << std::endl;
}

// 2. 向下类型转换
void test02()
{
    // 向下类型转换可能是危险的,非常容易出现越界内存写操作
    A* a = new A;
    D* d = static_cast<D*>(a);
    std::cout << a << " " << d << std::endl;

    B* b = new B;
    d = static_cast<D*>(b);
    std::cout << b << " " << d << std::endl;

    C* c = new C;
    d = static_cast<D*>(c);
    std::cout << c << " " << d << std::endl;
}

int main()
{
    //test01();
    test02();

    return 0;
}

普通继承中,基类子对象的位置固定,偏移量编译期就能确定,static_cast 可以完成转换。但在虚继承中,虚基类通常位于对象内存尾部,偏移要到运行期才能确定,编译期无法算出;因此 static_cast 禁止从虚基类向下转换,必须使用依赖 RTTI 的 dynamic_cast 在运行时修正指针偏移。

#include <iostream>
using namespace std;


// 虚基类指针向下转换
struct A { int a{ 0 }; };
struct B : virtual A { int b{ 0 }; };
struct C : B { int c{ 0 }; };

void test()
{
    // 虚基类指针向下转换: 不允许
    A* a = new C;
    //C* c = static_cast<C*>(a);
}

void test(A* a)
{
    // 普通继承不需要额外计算偏移,但在虚继承中,虚基类位于内存尾部,
    // 且不同派生路径会导致偏移量动态变化,编译阶段无法确定。
    // 因此,不能用 static_cast 进行向下转换,
    // 必须使用依赖 RTTI 的 dynamic_cast 来在运行时修正指针偏移。

    // B* b = static_cast<B*>(a);
}

int main()
{
    test();
    return 0;
}