相关疑难解决方法(0)

正确定义类型的方法(typedef vs #define)

哪些方法对于定义变量类型更安全?我知道当我们看到#defines时我们都皱着眉头,但它似乎和typedef一样有效:

有这样或那样的优势,如果是这样,它会是什么?

方法一:

    #include <iostream>

    #define byte unsigned char

    int main() {
        byte testByte = 'A';
        std::cout << testByte << std::endl;

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

方法二:

     #include <iostream>

     int main() {
        typedef unsigned char byte;

        byte testByte = 'A';
        std::cout << testByte << std::endl;

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

1
推荐指数
2
解决办法
1674
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1