在PJ Plauger的书"标准C库"中,他警告将函数指针赋给NULL.
具体来说,他说:
宏NULL 用作几乎通用的空指针常量.您将它用作数据对象指针的值,该指针应指向程序中未声明(或分配)的数据对象.正如我在第216页提到的,宏可以具有以下任何定义0,0L或(void*)0.
最后一个定义与任何数据对象指针兼容.但是,它与函数指针不兼容.这意味着你不能写
int (*pfun) (void) = NULL; /* WRONG */
翻译者可能会抱怨表达式类型与您要初始化的数据对象不兼容.
他继续说:
但是,无法保证指向void的指针与任何其他(非字符)指针具有相同的表示形式.它甚至不与函数指针兼容.这意味着您不能将NULL写为通用空指针常量.你也不能安全地将它用作参数表达式来代替任意数据对象指针.
我已经将函数指针分配NULL
了很长一段时间没有任何问题,我想知道它是否不可移植.
特别:
void (*test)() = NULL
=>用gcc和g ++编译好
void (*test)() = 0
=>用gcc和g ++编译好
void (*test)() = (void*)0
=>在gcc和g ++中都产生了无效的转换错误
编辑:void (*test)() = (void*)0
在gcc中编译很好,我使用的文件扩展名为.cpp ...尽管Plauger说分配函数指针NULL
是错误的,它仍然会编译吗?
我不明白的部分是我的stddef.h中NULL的定义:
#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else …
Run Code Online (Sandbox Code Playgroud) 我偶然发现了看起来像这样的旧代码:
void dothing(bool testBool,
const std::string& testString1,
const std::string& file,
int line,
const std::string& defaultString = "")
{
// do something...
}
void dothings(bool testBool,
const std::string& testString1,
const std::string& testString2,
const std::string& file,
int line,
const std::string& defaultString = "")
{
dothing(testBool, testString1, file, line, defaultString);
dothing(testBool, testString2, file, line, defaultString);
}
void dothings(bool testBool,
const std::string& testString1,
const std::string& testString2,
const std::string& testString3,
const std::string& file,
int line,
const std::string& defaultString = "")
{
dothings(testBool, testString1, testString2, file, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个类,我可以存储并使用类型信息,而无需模板参数.
我想写这样的东西:
class Example
{
public:
template<typename T>
Example(T* ptr)
: ptr(ptr)
{
// typedef T EnclosedType; I want this be a avaialable at the class level.
}
void operator()()
{
if(ptr == NULL)
return;
(*(EnclosedType*)ptr)(); // so i can cast the pointer and call the () operator if the class has one.
}
private:
void* ptr;
}
Run Code Online (Sandbox Code Playgroud)
我不是在问如何写一个is_functor()类.
我想知道如何在构造函数中获取类型信息并将其存储在类级别.如果这是不可能的,那么我们将不胜感激.
这个问题是关于Qt Installer Framework的2.0版本.
此时,使用Qt安装程序框架的人员通常都知道,如果没有自定义,您就无法通过安装程序覆盖现有安装.这显然是为了解决使用Qt框架完成此事时发生的一些问题.
但是,对于较小的,相对简单的项目,覆盖是完全正确的,并且比预先手动运行维护工具更方便.
我正在寻找一个涉及自定义UI +组件脚本的解决方案,该脚本向目标目录页面添加一个允许用户使用的按钮
最好是能够在目标目录中运行维护工具,让它自动删除给定的包,但我意识到这需要一点点.
我已阅读本网站上有关解决同一问题的其他问题的答案,但没有一个解决方案正常工作.我还想提一下,我有一个组件脚本启动并运行,但没有自定义UI.