c++ - 调用 Const 函数指针

标签 c++ function c++11 constants

首先,我想提一下,这适用于 MSVC,但不适用于 clang。我在 C++11 中使用 Clang。

我有一个函数指针:

typedef void (*Log) (const char* title, const char* msg, const char* file, int line);

我有这个结构:

 struct FunctionList
    {
    protected:
        static const int kNbrMax = 32;
        FunctionList();
        bool Add(const void* f);
        bool Remove(const void* f);

        const void*     m_functions[kNbrMax];
    };

这个类:

template<typename T>
struct MessageFunctionList
    : public FunctionList
{
public:
    MessageFunctionList(T defaultFunction)  
    {
        Add(defaultFunction);
    }

    void Call(const char* title,const char* cause,const char* file,int line)
    {
        for (unsigned long i = 0;i < m_nbrUsed;++i)
        {
            reinterpret_cast<T>(m_functions[i])(title,cause,file,line);
        }
    }
}

我是这样创建的:

static void DefaultLogMessageFunction(const char* title,const char* msg,const char* file,int line)
{

}

MessageFunctionList<Log> functionList(DefaultLogMessageFunction)

但是我得到编译时错误:

reinterpret_cast from 'const void ' to 'void ()(const char *, const char *, const char *, int)' casts away qualifiers for line: reinterpret_cast(m_functions[i])(title,cause,file,line);

据我所知,我正在尝试将我的 const 函数列表转换为非 const 值。这是不允许的,这是有道理的。所以我尝试了以下方法:

const void* funcPtr = m_functions[i];
const T call = reinterpret_cast<const T>(funcPtr);
call(title, cause, file, line);

但这也行不通。

这个有效:

void* funcPtr = const_cast<void*>(m_functions[i]);
T call = reinterpret_cast<T>(funcPtr);
call(title,cause,file,line);

但我想避免使用 const 转换。我究竟做错了什么?我怎样才能调用这个 const 函数?或者它根本不允许,因为它不知道被调用的函数是否是 const 函数?或者可能是因为我的静态函数不是类的成员,所以不能声明为 const?

最佳答案

您将函数指针存储为 const void* :

const void*     m_functions[kNbrMax];

而您正在尝试将它们转换为 T并使用 reinterpret_cast 调用它:

reinterpret_cast<T>(m_functions[i])(title,cause,file,line);

然而,reinterpret_cast无法删除 const来自类型的限定符,因此,您应该首先删除 const使用 const_cast :

reinterpret_cast<T>(const_cast<void*>(m_functions[i]))(title,cause,file,line);
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

但是,请注意 const_cast产生未定义的行为并且是不安全的。调用从 reinterpret_cast 返回的函数指针也是如此如果原始指针实际上不是 T .

编辑: 您可以调用 const然而,合格的函数指针则为 reinterpret_cast应包含 const限定词:

reinterpret_cast<const T>(m_functions[i])(title,cause,file,line);

关于c++ - 调用 Const 函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38748218/

相关文章:

C++ 保护 : fail to access base's protected member from within derived class

python pandas,一个函数将根据另一行的条件应用于一行中元素的组合

c++ - 使用 std::mutex 数组时,条件变量不会触发

c++ - 使用编译时已知的类型避免模板冗长和动态多态性

c++ - 混合部分模板特化和默认模板参数

c++ - 如何使用 winsock 的 send() 函数发送宽字符?

c++ - system() 以不同的方式执行 shell 命令 C++

c++ - 我的 c++filt 似乎不能正常工作,没有输出变化

C# 在字典中存储函数

c++ - 根据条件从函数模板返回不同的类型