Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 目标C中的函数指针_C++_Function Pointers - Fatal编程技术网

C++ 目标C中的函数指针

C++ 目标C中的函数指针,c++,function-pointers,C++,Function Pointers,我在调试时遇到了以下代码。我能够正确地得到结果,但我不理解这里定义的函数指针 此处pNewMsgeFunc是在结构strstrstruct中创建的函数指针tNewMsg的别名 RsMsg.h typedef RsMsg* (*tNewMsg)(void); tNewMsg pNewMsgFunc; typedef struct { int nMsgId; NSString* sAsciiName; tNewMsg

我在调试时遇到了以下代码。我能够正确地得到结果,但我不理解这里定义的函数指针

此处
pNewMsgeFunc
是在结构
strstrstruct
中创建的函数指针
tNewMsg
的别名

RsMsg.h

typedef RsMsg* (*tNewMsg)(void);
tNewMsg    pNewMsgFunc;
typedef struct
{
    int         nMsgId;       
    NSString*     sAsciiName;       
    tNewMsg    pNewMsgFunc; // calls new for the particular message
} stRsStruct;
RsMsg.cpp

RsMsg(int uMessageId,tNewMsg pNewMsg,const char* szAsciiName,void* pData,
      size_t uDataSize)
{
 //intialisations
}
RsMsgDerived.h

#define DECLARE_NEWMSG(CssName,CssID)                   \
    static CssName* FromMsg(RsMsg* pMsg)                \
    {                                                   \
        return dynamic_cast<CssName*>(pMsg);            \
    }                                                   \
    static RsMsg* NewMsg()                              \
    {                                                   \
        return new CssName;                             \
    }                                                   \
    enum {ID = CssID};                                  \
在这里,通过调用函数指针,我调用了静态
NewMsg()
函数

但是如何将此函数称为
pNewMsgFunc()
并没有分配
NewMsg
的地址

我需要通过
pNewMsgFunc()
调用
NewMsg()
函数。上述代码是否有任何更改

编辑:

如何在目标C中实现相同的代码。此函数指针调用函数 其返回类型为类。因此,尽管函数指针可以在C中实现为 它调用返回类型为类的函数无法在目标C中实现 作为c函数。

tNewMsg    pNewMessageFunc
不创建别名,它声明tNewMsg类型的存储位置。我怀疑您在此处输入的代码不正确,因为在头文件中声明存储位置通常会导致重复的存储位置(即链接器错误)

这看起来像一个构造函数

RsMsg(int uMessageId,tNewMsg pNewMsg,const char* szAsciiName,void* pData,size_t uDataSize)
{
    //intialisations
}

它接受一个tNewMsg,并可能将其存储在类的实例数据中。因此,您的
RsMsg::CreateMessage
函数可能有权访问初始化的函数指针

RsMsg(int uMessageId,tNewMsg pNewMsg //this the function pointer as an arg to your initialization function, so I'm guessing the address of the correct function is passed here.
只是一个猜测,从你发布的代码看不出来

这里,pNewMsgeFunc是在strstrstruct结构内部创建的函数指针tNewMsg的别名

不,不是
tNewMsg
是标识类型的名称

typedef RsMsg* (*tNewMsg)(void);
…但是
pNewMessageFunc
是该类型的对象

这样看。在此代码中:

typedef unsigned int uint;
uint n = 42;
n
不是
uint
的别名。相反,
n
是类型为
uint
的变量


因此,
pNewMessageFunc
是一个全局变量(类型为pointer to function,不带参数,返回指向-
RsMsg
)的指针,您永远不会初始化它。

我首先想到的是,您将指针分配给一个
strstruct
,但在分配它指向的内存之前使用它。这是未定义的行为,您可以期待任何事情发生。这可能是因为,由于其他调用,它恰好碰到了堆栈的一部分,其中另一个函数有一个指向实际内存的类似指针,而您只是碰到了一些旧的东西。为了使任何事情都能重复发生,在不将指针指向某个实际内存块的情况下,决不能取消对指针的引用。

最有可能是objective-c模块文件。您能清理一些东西吗?您没有告诉我们什么平台(Objective-C++?),这在说明事情为何会出现行为方面可能很重要。您列出的代码示例看起来很奇怪:您在哪里使用
DECLARE\u NEWMSG
?此外,标题几乎没有任何意义。
RsMsg::CreateMessage
正如您所写,几乎肯定会出错。指针
pMsgStruct
未初始化,因此它可能指向内存中的任何位置。然后
pMsgStruct->pNewMsgFunc()。你应该发布更多的真实代码,这样人们就可以看到正在发生的事情。解释一下strstrstruct的匈牙利符号。
strstrstruct
。感谢你清楚地解释我。我已经编辑了这篇文章,请看。
typedef unsigned int uint;
uint n = 42;