簡體   English   中英

如果指定了參數,如何為函數指針分配地址?

[英]How to assign address to a function pointer if parameters is specified?

在此之前,我曾使用過如此巨大的東西:

int* ShowPopUpMessage = (int*)0x004837F0; //declare
((int (__cdecl*)(const char *, char))ShowPopUpMessage)("You see this message!", 0); //call

嗯,我認為不必說這有點令人困惑。

現在,我想像這樣聲明普通的函數指針:

int *ShowPopupMessage(const char *, char);

為了能夠像這樣調用該函數:

ShowPopupMessage("asd", 0);

但是我不能將函數地址分配給該指針。 我試過了:

int *ShowPopupMessage(const char *, char) = (int*)0x004837F0; //error: function "ShowPopupMessage" may not be initialized

//then this
int *ShowPopupMessage(const char *, char);
ShowPopupMessage = 0x004837F0; //nope

ShowPopupMessage = (int*)0x004837F0; //nope

*ShowPopupMessage = 0x004837F0; //nope

*ShowPopupMessage = (int*)0x004837F0; //nope

還有其他方法嗎?

即使隨后嘗試解決編譯錯誤,您實際上也應該閱讀編譯器的診斷信息。 真的懷疑它曾經說過“不”。

當您要聲明一個返回函數指針的函數(或函數指針)時, typedef特別有用。 在一個簡單的函數指針類型表達式中,它並不會花很多錢,因為您實際上只需要在寫括號時記住括號即可,例如:

int *ShowPopupMessage(const char*, char); // a function declaration
int (*ShowPopupMessage)(const char*, char); // a function pointer definition

兩者都是C ++程序員的輕松閱讀。

在您的情況下,最好使用typedef (因為您需要兩次使用該類型),但是由於您似乎難以理解可接受的隱式轉換,因此我不會將問題隱藏在typedef幕后。

在上面的示例中,您基本上只更改了事物的左側。 C ++不允許將整數類型隱式轉換為(任何)指針類型,並且以類似的方式,它不允許將對象指針類型隱式轉換為函數指針類型。 如果要將整數解釋為函數指針,則需要進行強制轉換-不僅需要強制轉換,還需要將reinterpret_cast轉換為函數指針類型。

// this is OK (with the cast), but ShowPopupMessage is not a function pointer,
// but a pointer to int
int *ShowPopupMessage = (int*)0xDEADBEEF;

// this is incorrect, as C++ does not allow implicit conversions from
// object pointers to function pointers
int (*ShowPopupMessage)(const char*, char) = (int*)0xDEADBEEF;

// this is OK (assuming you know what you're doing with 0xDEADBEEF)
int (*ShowPopupMessage)(const char*, char) =
        (int(*)(const char*, char))0xDEADBEEF;

// this is preferable in C++ (but it's not valid C)
int (*ShowPopupMessage)(const char*, char) =
        reinterpret_cast<int(*)(const char*, char)>(0xDEADBEEF);

// (this is a possibility in C++11)
#include <functional>
std::function<int(const char*, char)> ShowPopupMessage =
        reinterpret_cast<int(*)(const char*, char)>(0xDEADBEEF);
// it's used in much the same way as function pointers are
// i.e. you call it like you always call them: ShowPopupMessage("", ' ')

使用typedef:

typedef int (*funptr)(const char *, char);
funptr ShowPopupMessage;

ShowPopupMessage = (funptr) your_address_goes_here;
ShowPopupMessage("hello", 0);

這應該工作:

{
    typedef int (*funcPtr)(const char *, char);

    funcPtr func = (funcPtr) 0x004837F0;

    func("asd", 0);
}

這是一個函數聲明,而不是函數指針聲明:

int *ShowPopupMessage(const char *, char);

使用typedef創建函數指針:

typedef int (*func_t)(const char*, char);

可以使用:

int _my_pop_up_one(const char* a_msg, char a_ch)
{
    std::cout << "one: " << a_msg << ", " << a_ch << "\n";
    return 0;
}

int _my_pop_up_two(const char* a_msg, char a_ch)
{
    std::cout << "two: " << a_msg << ", " << a_ch << "\n";
    return 0;
}

func_t show = _my_pop_up_one;
show("hello", 'a');
show = _my_pop_up_two;
show("hello", 'a');

因為這是C ++,值得一提的是函子 (和std::string代替char* )的存在,它們允許對象像普通函數一樣被調用:

struct func_t
{
    virtual int operator()(const std::string&, char) const = 0;
};

struct my_pop_up_one : func_t
{
    int operator()(const std::string& a_msg, char a_ch) const
    {
        std::cout << "one: " << a_msg << ", " << a_ch << "\n";
        return 0;
    }
};

struct my_pop_up_two : func_t
{
    int operator()(const std::string& a_msg, char a_ch) const
    {
        std::cout << "two: " << a_msg << ", " << a_ch << "\n";
        return 0;
    }
};

void display_pop_up(const func_t& a_func)
{
    a_func("hello", 'a');
}

display_pop_up(my_pop_up_one());
display_pop_up(my_pop_up_two());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM