小编bob*_*bay的帖子

为什么要转换为指针然后取消引用?

我正在通过这个例子,它有一个函数输出一个十六进制位模式来表示任意浮点数.

void ExamineFloat(float fValue)
{
    printf("%08lx\n", *(unsigned long *)&fValue);
}
Run Code Online (Sandbox Code Playgroud)

为什么要取fValue的地址,转换为无符号长指针,然后取消引用?是不是所有的工作都等同于直接转换为无符号长?

printf("%08lx\n", (unsigned long)fValue);
Run Code Online (Sandbox Code Playgroud)

我试过了,答案不一样,很困惑.

c c++ casting

19
推荐指数
1
解决办法
2448
查看次数

我应该将许多函数中使用的变量声明为成员变量吗?

我在课堂上有很多重载的功能.在这种情况下,我应该将int32_t数据声明为类的成员变量,所以我不是在每个函数中反复声明它吗?Fill函数总是通过引用设置一个值,所以我认为我不应该每次都在每个函数中声明它.

这里没有列出大约20多个这些功能:

void TransmitInfo(TypeA &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))  //Fill accepts a reference variable, "data" gets populated
    {
        Transmit(tc, data);  
    }
}

void TransmitInfo(TypeB &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))
    {
        Transmit(tc, data);  
    }
}

void TransmitInfo(TypeC &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))
    {
        Transmit(tc, data);  
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
66
查看次数

宏内部的静态变量

我正在看一些代码并认为整个逻辑在这里没有意义.拥有像这样的宏似乎很差.作为一个例子,我使用这个宏两次,我得到两个字符串输出.

TESTING("hello");
TESTING("world");
Run Code Online (Sandbox Code Playgroud)
  1. 预处理器只是用代码块替换标识符,它与复制/粘贴代码相同,因此如果多次使用此宏,编译器是否应该为firstPass提供重新定义错误?(如果多次手动粘贴main,static bool firstPass = true;编译器将给出错误.)

  2. 如果firstPassstatic,则仅创建并初始化一次.因此,多次使用此宏,不应该产生任何结果(除了第一次使用),因为标志设置为false,但它仍然cout每次都这样做.

有点困惑.

#define TESTING(input)                           \
{                                                \
   static bool firstPass = true;                 \
   if (firstPass) {                              \
     std::cout << input;                         \
     firstPass = false;                          \
   }                                             \
}
Run Code Online (Sandbox Code Playgroud)

c++ macros c-preprocessor

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

标签 统计

c++ ×3

c ×1

c-preprocessor ×1

casting ×1

macros ×1