在同一个 class 中调用函数指针

Calling a function pointer inside the same class

我几乎但没有完全在我的 class 中实现函数指针以避免大循环的代码重复。

class Scanner
{

    void GenerateTextureMap();
    void OtherOuterWork();

    /* 
        Extracting the triangle sweep for loop (that is repeated code for each time we need to sweep the triangles) 
        Of note:
            - "How can I avoid syntax errors when creating pointers to members? Use a typedef" https://isocpp.org/wiki/faq/pointers-to-members#typedef-for-ptr-to-memfn
            - "How can I avoid syntax errors when calling a member function using a pointer-to-member-function? use std::invoke (C++17)" https://isocpp.org/wiki/faq/pointers-to-members#macro-for-ptr-to-memfn 
    */
    typedef  int (Scanner::*TriangleSweepFunction)(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p);
    void TriangleSweep(TriangleSweepFunction p);

    void MatchCameraToTrianglePaintTextureTriangle(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p);
    void OtherInnerWork(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p);

}

void Scanner::TriangleSweep(TriangleSweepFunction p)
{
    for (int triangleIndex = 0; triangleIndex < m_mesh.m_triangles.size(); triangleIndex++)
    {
        // ...
        // loads of code
        // ...

        std::invoke(p, this, triangleIndex, texv0p, texv1p, texv2p);
    }
}

void Scanner::MatchCameraToTrianglePaintTextureTriangle(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p)
{
    // Inner work on Scanner object variables
    // ...
}

void Scanner::GenerateTextureMap()
{       
    TriangleSweep(&Scanner::MatchCameraToTrianglePaintTextureTriangle);
}

TriangleSweep(&Scanner::MatchCameraToTrianglePaintTextureTriangle);

产量

E0167 argument of type "void (Scanner::*)(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p)" is incompatible with parameter of type "Scanner::TriangleSweepFunction"

Error C2664 'void Scanner::TriangleSweep(Scanner::TriangleSweepFunction)': cannot convert argument 1 from 'void (__cdecl Scanner::* )(int,Vec2D,Vec2D,Vec2D)' to 'Scanner::TriangleSweepFunction'

    TriangleSweepFunction p = &Scanner::MatchCameraToTrianglePaintTextureTriangle;
    TriangleSweep(p);

产量

E0144 a value of type "void (Scanner::*)(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p)" cannot be used to initialize an entity of type "Scanner::TriangleSweepFunction"

这里的正确语法是什么?(利用已经定义的 typedef)

打字错误:

typedef int (Scanner:: 应该是 typedef void (Scanner:: 因为我的函数 return void (void MatchCameraToTrianglePaintTextureTriangle( void OtherInnerWork()

问题是,您创建的是函数指针,而不是指向 int 的指针(请注意 class 定义中不需要 Scanner:: 作用域运算符):

typedef int (Scanner::*TriangleSweepFunction)(int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p); void TriangleSweep(TriangleSweepFunction p);

您需要做的:

typedef int * TriangleSweepFunction (int triangleIndex, Vec2D texv0p, Vec2D texv1p, Vec2D texv2p); void TriangleSweep(TriangleSweepFunction p);

//函数returns int指针

int * fun ( int, int); 

//创建一个指向a
的函数指针 //函数类型 returns int!

int (* fun) (int , int);

注:函数两边的括号 创建函数指针时名称非常重要