c++ - 声明一个变量内联赋值C++

标签 c++ variables syntax bitmap inline

在阅读 C++ 中的位图处理时,我遇到了这段代码 用于使用从位图文件中获取的数据加载调色板:

//set Number of Colors
numColors = 1 << bmih.biBitCount;

//load the palette for 8 bits per pixel
if(bmih.biBitCount == 8) {
        colours=new RGBQUAD[numColours];
        fread(colours,sizeof(RGBQUAD),numColours,in);
}

其中“bmih.biBitCount”是一个已经有值的预定义变量。 为什么作者声明 numColors 等于 1,然后在同一行中将值 bmih.biBitCount 赋给该变量?这到底是做什么的?像这样内联两次为变量赋值有什么好处?

最佳答案

Why does the author declare numColors to equal 1 then assign the value bmih.biBitCount to that variable in the same line?

他没有;他分配表达式 1 << bmih.biBitCount 的结果至 numColors .分配发生在最后。 <<bitwise left shift operator .这样想:

//set Number of Colors
numColors = (1 << bmih.biBitCount);

关于c++ - 声明一个变量内联赋值C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358072/

相关文章:

c++ - 如何正确使用带有智能指针和自定义类的 map 作为键和值

c++ - 在 C++ 中,我们可以使用 { } 进行 C 风格转换吗?

c++ - UE4打开VS2017两次

Javascript 将变量设置为输入,然后导航到页面

C 变量初始化 valgrind 提示

python - 如何在 python 中使用生成器制作嵌套 for 循环?

javascript - 对于内部包含一项功能的 if 语句,最适合使用哪种语法?

c++ - 如何构造一个 std::string 具有可变数量的空格?

JavaScript,声明变量时语法困惑

被C语法搞糊涂了