在我的头文件中,可以创建一个指向另一个cpp文件中函数的指针吗?

huangapple go评论56阅读模式
英文:

Can I create a pointer in my header file, pointing to a function in another cpp file?

问题

我在我的abc.cpp文件中有一个函数

```cpp
namespace myNamespace{

void Gif::myfunc(){
//code
}

}

在另一个Header.h文件中:

void createPointer(){

void(*funcPtr)()=&Gif::myfunc;

}

我可以这样做吗,或者类似的操作?

注意:这不是abc.h,而是另一个头文件,其中没有myfunc()的声明。
Gif是一个类。
由于我将在构建时使用perl脚本创建另一个Header.h,因此我不能使用相关的头文件。

我对C++还不太了解,因此很难理解命名空间/类/指针的范围。

我只需要知道是否可以在头文件中访问另一个源文件中定义的函数,并保存它们的指针。我将在abc.cpp文件中再次使用这个设置的指针。我知道最好的方法是在abc.cpp中定义函数指针,但由于项目的需求,我不能这样做。

我究竟想要做什么:
一个类中有多个设置函数(100+),我正在动态创建一个头文件,该头文件将创建一个映射,以函数名为键,并存储在abc.cpp中的函数指针。我将使用这个映射通过键调用这些函数,在我的abc.cpp文件中。```

英文:

I have a func in my abc.cpp file

namespace myNamespace{

void Gif::myfunc(){
//code
}

}

In anotherHeader.h file:

void createPointer(){

void(*funcPtr)()=&Gif::myfunc;

}

can I do this, or anything similar?

Note: This is not abc.h, it is another header file which does not have myfunc() declaration.
Gif is a class.
I cannot use the relevant header file, since I'll be creating this anotherHeader.h using a perl script during build time.

I'm new to C++, hence it is difficult to understand the scope of namespaces/classes/pointers.

I just need to know if I can access the functions defined in another source file, in a header file and save their pointer. I will be using this set pointer again in abc.cpp file. I know the best way is to define the function pointer in abc.cpp itself, but I cannot do that, due to my project needs.

what exactly am I trying to do:
There are multiple set functions(100+) present one class, I am creating a header file dynamically which will create a map, with keys as func names and store the function pointers present in abc.cpp. I will use this map to call these functions in my abc.cpp fil using the keys.

答案1

得分: 1

anotherHeader.h 应该有这个函数的声明。

例如,在 abc.h 中:

#pragma once
class Gif
{
public:
    static void myfunc();
};

然后在你的 anotherHeader.h 文件中,只需包含 abc.h。

英文:

The anotherHeader.h should have the declaration of this function.

For example, in abc.h

#pragma once
class Gif
{
public:
    static void myfunc();
};

Then in your anotherHeader.h file, just include abc.h.

答案2

得分: 1

是的,但不是以确切的方式。你会使用 void(Gif::*funcPtr)()=&amp;Gif::myfunc,而不是 void(*funcPtr)()=&amp;Gif::myfunc。然而,我相信 Gif 类是在命名空间中的,这种情况下会是 void(myNamespace::Gif::*funcPtr)()=&amp;Gif::myfunc。这是因为类型为 void (myNamespace::Gif::*)() 的值不能用于初始化类型为 void (*)() 的实体 <- 这个变量。

编辑: 为了使你的代码工作,你需要在头文件中包含 .cpp 文件。根据C++的指导方针,这不是理想的策略,因为这样做会在技术上交换角色。然而: 与其这样做,一个更理想的方法是将 createPointer 包含在命名空间中,如果可能的话,像这样:

// anotherHeader.h
namespace myNamespace
{
    void createPointer()
    {
        // ... 你的代码
    }
}

然后,在 cpp 文件中包含头文件:#include &quot;anotherHeader.h&quot;

英文:

Yeah, but not in the exact way. Instead of void(*funcPtr)()=&amp;Gif::myfunc, you would do void(Gif:: * funcPtr)()=&amp;Gif::myfunc. However, I believe the Gif class is in the namespace, in which case it would be void(myNamespace::Gif:: * funcPtr)()=&amp;Gif::myfunc. This is because a value of type void (myNamespace::Gif::*)() cannot be used to initialize an entity of type void (*)() <- the variable.

Edit: For your code work, you would have needed to include the .cpp file in the header file. That is not the ideal strategy based off guidelines in C++, as the roles are technically switched by doing so. HOWEVER: instead of this, a more ideal way of doing this would do be to include createPointer in the namespace like this, if possible:

// anotherHeader.h
namespace myNamespace
{
    void createPointer()
    {
        // ... your code
    }
}

Then, you include the header file in the cpp: #include &quot;anotherHeader.h&quot;

huangapple
  • 本文由 发表于 2023年6月12日 10:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定