C++类型转换之static_cast

前言

 static_cast是可以使用的最简单的类型转换。它是编译时强制转换。它可以在类型之间进行隐式转换(例如int到float,或指针到void*),它还可以调用显式转换函数(或隐式转换函数)。

const_cast用法示例

下面是static_cast的11个使用场景示例:

1. 用于原C风格的隐式类型转换

 例如float转int

   float a = 1.3;
    int b = static_cast<int>(a);
    cout<<"b="<<b<<endl;

2.静态下行转换( static downcast)

 不执行类型安全检查。

Note:

If new-type is a reference to some class D and expression is an lvalue of its non-virtual base B, or new-type is a pointer to some complete class D and expression is a prvalue pointer to its non-virtual base B, static_cast performs a downcast. (This downcast is ill-formed if B is ambiguous, inaccessible, or virtual base (or a base of a virtual base) of D.)

Such a downcast makes no runtime checks to ensure that the object’s runtime type is actually D, and may only be used safely if this precondition is guaranteed by other means, such as when implementing static polymorphism. Safe downcast may be done with dynamic_cast. If the object expression refers or points to is actually a base class subobject of an object of type D, the result refers to the enclosing object of type D. Otherwise, the behavior is undefined.

struct B
{
    
    
    int m = 42;
    const char *hello() const
    {
    
    
        return "Hello world, this is B!\n";
    }
};

struct D : B
{
    
    
    const char *hello() const
    {
    
    
        return "Hello world, this is D!\n";
    }
};

 将父类的引用转换为子类的引用

    D d;
    B &br = d; // upcast via implicit conversion
    std::cout << "1) " << br.hello();
    D &another_d = static_cast<D &>(br); // downcast
    std::cout << "1) " << another_d.hello();

 控制台输出

1) Hello world, this is B!
1) Hello world, this is D!

3.左值转换为右值引用(lvalue to xvalue)

 将左值v0的资源转移到右值引用v2, v2为{1, 2, 3}的右值引用

	std::vector<int> v0{
    
    1, 2, 3};
    std::vector<int> v2 = static_cast<std::vector<int> &&>(v0);
    std::cout << "2) after move, v0.size() = " << v0.size() << '\n';

 控制台输出

3) after move, v0.size() = 0

4.初始化转换(initializing conversion)

 在变量初始化期间就对初始化数据进行类型转换

   // 4. initializing conversion
    int n = static_cast<int>(3.14);
    std::cout << "4) n = " << n << '\n';
    std::vector<int> v = static_cast<std::vector<int>>(10);
    std::cout << "4) v.size() = " << v.size() << '\n';

 控制台输出

4) n = 3
4) v.size() = 10

5.转换为void并丢弃

 如果 new type为void类型,static_cast将会在计算表达式的值之后丢弃这个值,无法使用变量接到这个值。

static_cast<void>(v2.size());  
int a =  static_cast<void>(v2.size());  //error,void value not ignored as it ought to be

6.上行转换,同时将数组转换为指针

struct B
{
    
    
    int m = 42;
    const char *hello() const
    {
    
    
        return "Hello world, this is B!\n";
    }
};

struct D : B
{
    
    
    const char *hello() const
    {
    
    
        return "Hello world, this is D!\n";
    }
};

 将子类对象数组转换为了父类指针,执行类型安全检查。

   D a[10];
   cout<<a->hello();
   [[maybe_unused]] B *dp = static_cast<B *>(a);
   cout<<dp->hello();

 控制台输出

Hello world, this is D!
Hello world, this is B!

8.void*转换到具体类型( inverse of implicit conversion)

 static_cast可以提取void*类型中的值

  void *nv = &n;

  int *ni = static_cast<int *>(nv);

  std::cout << "6) *ni = " << *ni << '\n';

 控制台输出

6) *ni = 3

9.枚举转int(scoped enum to int)

 将枚举代表的值转换为int

enum class E
{
    
    
    ONE = 1,
    TWO,
    THREE
};
   E e = E::TWO;
   int two = static_cast<int>(e);
   std::cout << "7) " << two << '\n';

10.int转enum以及enum转为其他enum(int to enum, enum to another enum)

enum class E
{
    
    
    ONE = 1,
    TWO,
    THREE
};
enum EU
{
    
    
    ONE = 1,
    TWO,
    THREE
};

   E e2 = static_cast<E>(two);
   [[maybe_unused]] EU eu = static_cast<EU>(e2);

11.成员指针的上行转换(pointer to member upcast)

  将D内的成员变量的指针转换为B类型的成员变量指针

   int D::*pm = &D::m;
   std::cout << "10) " << br.*static_cast<int B::*>(pm) << '\n';

 控制台输出

10) 42

《C++ Primer》《Effective C++》是C++开发者必不可少的书籍,如果你想入门C++,以及想要精进C++开发技术,这两本书可以说必须要有。此外,《Linux高性能服务器编程》以及《Linux多线程服务端编程:使用muduo C++网络库》.(陈硕)》是快速提高你的linux开发能力的秘籍。在网上搜索相关资源也要花费一些力气,需要的同学可以关注公众号**【程序员DeRozan】,回复【1207】**快速免费领取~

猜你喜欢

转载自blog.csdn.net/dddgggd/article/details/129348411