reinterpret_cast<> 理解以及典型应用[cpp][类型转换]

1 ffmpeg 解帧

ffmpeg -i DJI_20210615164633_0003_W.MP4 -r 3 images/%4d.jpg

2 reinterpret_cast<> 理解以及典型应用:

对于其他的例如static_cast<>等的应用,参考:http://www.cplusplus.com/doc/tutorial/typecasting/

以下引用自: https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast

Here is a variant of Avi Ginsburg’s program which clearly illustrates the property of reinterpret_cast mentioned by Chris Luengo, flodin, and cmdLP: that the compiler treats the pointed-to memory location as if it were an object of the new type:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class A
{
    
    
public:
    int i;
};

class B : public A
{
    
    
public:
    virtual void f() {
    
    }
};

int main()
{
    
    
    string s;
    B b;
    b.i = 0;
    A* as = static_cast<A*>(&b);
    A* ar = reinterpret_cast<A*>(&b);
    B* c = reinterpret_cast<B*>(ar);
    
    cout << "as->i = " << hex << setfill('0')  << as->i << "\n";
    cout << "ar->i = " << ar->i << "\n";
    cout << "b.i   = " << b.i << "\n";
    cout << "c->i  = " << c->i << "\n";
    cout << "\n";
    cout << "&(as->i) = " << &(as->i) << "\n";
    cout << "&(ar->i) = " << &(ar->i) << "\n";
    cout << "&(b.i) = " << &(b.i) << "\n";
    cout << "&(c->i) = " << &(c->i) << "\n";
    cout << "\n";
    cout << "&b = " << &b << "\n";
    cout << "as = " << as << "\n";
    cout << "ar = " << ar << "\n";
    cout << "c  = " << c  << "\n";
    
    cout << "Press ENTER to exit.\n";
    getline(cin,s);
}

Which results in output like this:

as->i = 0
ar->i = 50ee64
b.i   = 0
c->i  = 0

&(as->i) = 00EFF978
&(ar->i) = 00EFF974
&(b.i)   = 00EFF978
&(c->i)  = 00EFF978

&b = 00EFF974
as = 00EFF978
ar = 00EFF974
c  = 00EFF974
Press ENTER to exit.

It can be seen that the B object is built in memory as B-specific data first, followed by the embedded A object. The static_cast correctly returns the address of the embedded A object, and the pointer created by static_cast correctly gives the value of the data field. The pointer generated by reinterpret_cast treats b’s memory location as if it were a plain A object, and so when the pointer tries to get the data field it returns some B-specific data as if it were the contents of this field.

应用如下:
下面的应用根据输入的类型T判断后使用了reinterpret_cast去转换;

template <typename T>
void OptionManager::RegisterOption(const std::string& name, const T* option) {
    
    
  if (std::is_same<T, bool>::value) {
    
    
    options_bool_.emplace_back(name, reinterpret_cast<const bool*>(option));
  } else if (std::is_same<T, int>::value) {
    
    
    options_int_.emplace_back(name, reinterpret_cast<const int*>(option));
  } else if (std::is_same<T, double>::value) {
    
    
    options_double_.emplace_back(name, reinterpret_cast<const double*>(option));
  } else if (std::is_same<T, std::string>::value) {
    
    
    options_string_.emplace_back(name,
                                 reinterpret_cast<const std::string*>(option));
  } else {
    
    
    LOG(FATAL) << "Unsupported option type";
  }
}

Guess you like

Origin blog.csdn.net/cxy_hust/article/details/119970946