C++ Cast And Go Cast

C++

  • A static_cast can be used to explicitly convert between related pointer types, such
    as void* and double*
  • reinterpret_cast can cast between unrelated types, such as int and double* .
  • const_cast can “cast away const .”

For example:

int* pi = static_cast<int*>(pv);
Register* in = reinterpret_cast<Register*>(0xff);
void f(const Buffer* p)
{
    Buffer* b = const_cast<Buffer*>(p);
    // . . .
}

Go Cast

There are three methods as follows:

  • use ()
    example: var b float64 = float64(a)
  • stcrcov package
    example: num, err := strconv.Atoi(str)
  • interface{} type
    example: str, ok := i.(string)
posted @ 2023-08-16 18:03  名白  阅读(8)  评论(0编辑  收藏  举报