标签: member-function-pointers

C++类成员函数和来自C API的回调

我正在尝试学习如何write_data(…)funmain()类中的函数调用此函数,如下面的代码所示.(我知道如果我只列出这两个函数而不将它放在一个类中,这个程序就可以工作).

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data)line给了我错误,不允许我调用write_data(...)函数.你可以请更正我的代码并告诉我如何实现这一目标.任何帮助将不胜感激.谢谢.

error C3867: 'go_website::write_data': function call missing argument list; use '&go_website::write_data' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
//Microsoft Visual Studio 10 in C++
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <iostream>
#include <curl/easy.h>
#include <string>
using namespace std;

extern "C" typedef size_t curl_write_callback(void *ptr, size_t size, size_t nmemb, FILE *stream);
class go_website
{
public:
static curl_write_callback write_data;

void funmain()
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://www.shorturl.com/"; …
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers callback member-functions

4
推荐指数
1
解决办法
4612
查看次数

&decltype(obj)::成员无法正常工作

为什么这不起作用(Visual C++ 2012 Update 1),以及解决它的正确方法是什么?

#include <boost/lambda/bind.hpp>
namespace bll = boost::lambda;

struct Adder
{
    int m;
    Adder(int m = 0) : m(m) { }
    int foo(int n) const { return m + n; }
};

#define bindm(obj, f, ...)  bind(&decltype(obj)::f, obj, __VA_ARGS__)

int main()
{
    return bll::bindm(Adder(5), foo, bll::_1)(5);
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers decltype c++11

4
推荐指数
1
解决办法
135
查看次数

C++字符串和成员函数指针的映射

嘿所以我正在制作一个以字符串作为键和成员函数指针作为值的映射.我似乎无法弄清楚如何添加到地图,这似乎没有工作.

#include <iostream>
#include <map>
using namespace std;

typedef string(Test::*myFunc)(string);
typedef map<string, myFunc> MyMap;


class Test
{
private:
    MyMap myMap;

public:
    Test(void);
    string TestFunc(string input);
};





#include "Test.h"

Test::Test(void)
{
    myMap.insert("test", &TestFunc);
    myMap["test"] = &TestFunc;
}

string Test::TestFunc(string input)
{
}
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers map

4
推荐指数
1
解决办法
1万
查看次数

QT SLOT:成员函数指针错误

我目前正在开发一个Qt项目,我在SLOT方面遇到了一些麻烦.我想传递一个指向成员函数的指针作为SLOT的参数.为此,我在课堂上宣布了SLOT,但是当我这样做时,我得到了MOC错误.我不知道我想要达到的目标是否可行.

名为MainFrame的类的语法示例:

void slotAnswerReceived (QString answer, void (MainFrame::*ptr)(QString));
Run Code Online (Sandbox Code Playgroud)

我没有任何连接,没有使用该功能,我唯一的错误是在上面的这一行.

谢谢大家的帮助.我在网上找不到任何解决方案(但是如果有人感兴趣,我发现这篇文章深入解释了SIGNAL和SLOT).

c++ qt member-function-pointers signals-slots slot

4
推荐指数
1
解决办法
1881
查看次数

调用成员函数指针

我在调用结构内的函数指针时遇到问题。我之前在类之外使用过这种方法,但现在我正在使用指向其他类方法的函数指针在类方法中尝试它......我收到一个编译器错误。这是我的课:

class Myclass
{
    int i;

    void cmd1(int)
    {}

    void cmd2(int)
    {}

    void trans()
    {
        const struct
        {
            std::string cmd;
            void (Myclass::*func)(int)
        }
        CmdTable[] =
        {
            { "command1", &Myclass::cmd1 },
            { "command2", &Myclass::cmd2 }
        };

        CmdTable[0].func(i);
        CmdTable[1].func(i);
    }
};
Run Code Online (Sandbox Code Playgroud)

线CmdTable[0].func(i);CmdTable[1].func(i);两个提供下列错误:错误:表达式必须具有(指针TO-)函数类型。

我意识到可能有更好的方法来做到这一点,但我很好奇为什么我写的东西不起作用。任何解释将不胜感激。

c++ pointers member-function-pointers

4
推荐指数
1
解决办法
92
查看次数

如何在 C++11 中存储任意方法指针?

我需要一种方法来存储方法指针列表,但我不在乎它们属于哪个类。我想到了这一点:

struct MethodPointer
{
    void *object;
    void (*method)(void);
};
Run Code Online (Sandbox Code Playgroud)

然后我可以有一个采用任意方法的函数:

template <typename T>
void register_method(void(T::*method)(void), T* obj) {
    MethodPointer pointer = {obj, method);

}

void use_method_pointer() {
    ...
    MethodPointer mp = ...

    // call the method
    (mp.object->*method)();

    ...
}
Run Code Online (Sandbox Code Playgroud)

这显然无法编译,因为我无法在 register_method() 中将方法指针转换为函数指针。

我需要这个的原因是因为我有一个可以发出事件的类 - 我希望任意实例订阅这些事件作为方法调用。这是可能的吗?

附注。适用条件: 1. 我不想使用 Boost 2. 我不想使用“侦听器”接口,其中订阅者必须对抽象接口类进行子类化。

感谢您的时间。

c++ store member-function-pointers list c++11

4
推荐指数
1
解决办法
1041
查看次数

从非类型模板参数确定类型

我希望我能做到这一点:

template <typename T x>
struct Test {
    T val{x};
};

int main() {
    Test<3> test;
    return test.val;
}
Run Code Online (Sandbox Code Playgroud)

但我不能.对?


我在这里回答了一个问题,我使用以下模板:

template <typename T, typename V, typename VP, V(T::*getf)(), void (T::*setf)(VP)>
Run Code Online (Sandbox Code Playgroud)

每种类型都是手动指定的.但这是一个重复因为T,V并且VP已经包含在指向成员函数getfsetf类型的指针中.


但是,如果我只尝试模板

template <V(T::*getf)(), void (T::*setf)(VP)>
Run Code Online (Sandbox Code Playgroud)

要么

template <V(T::*getf)(), void (T::*setf)(VP), typename T, typename V, typename VP>
Run Code Online (Sandbox Code Playgroud)

那么类型无法确定.


接下来我尝试了专业化:

template <typename T, typename T2>
struct Accessor;

template <typename V, typename T, typename VP>
struct Accessor <V(T::*)(), void …
Run Code Online (Sandbox Code Playgroud)

c++ templates member-function-pointers

4
推荐指数
1
解决办法
255
查看次数

避免非类类型的指向成员函数

我正在编写一种容器类,我想提供一种apply方法来评估容器内容的函数.

template<typename T>
struct Foo
{
    T val;

    /** apply a free function */
    template<typename U> Foo<U> apply(U(*fun)(const T&))
    {
        return Foo<U>(fun(val));
    }

    /** apply a member function */
    template<typename U> Foo<U> apply(U (T::*fun)() const)
    {
        return Foo<U>((val.*fun)());
    }
};

struct Bar{};
template class Foo<Bar>; // this compiles
//template class Foo<int>; // this produces an error
Run Code Online (Sandbox Code Playgroud)

最后一行产生error: creating pointer to member function of non-class type ‘const int’.虽然我只实例化Foo,而不是用apply在所有.所以我的问题是:如果T是非类型类型,我怎样才能有效地删除第二个重载?

注意:我也试过只有一个重载 …

c++ templates member-function-pointers function-pointers

4
推荐指数
1
解决办法
165
查看次数

如何声明恒定的方法指针

1 ST关断这不是关于如何指向一个恒定方法问题。我想知道如何使我的方法指针恒定。

鉴于:

struct foo {
    void func1();
    void func2();
};
Run Code Online (Sandbox Code Playgroud)

我可以使用构建方法指针,void (foo::*bar)() = &foo::func1 但是以后可以使用,bar = &foo.func2我想避免这种情况。

我可以轻松地做到这一点const auto bar = &foo::func1,但是我不确定在之前如何做到这一点。

c++ member-function-pointers function-pointers constants c++03

4
推荐指数
1
解决办法
58
查看次数

如何将重载的成员函数作为参数传递?

这是我面临的问题:我在一个类中有一个重载函数,并且我想将其重载之一作为参数传递。但是这样做时,出现以下错误:

"no suitable constructor exists to convert from <unknown-type> to std::function<...>"
Run Code Online (Sandbox Code Playgroud)

这是一个代码示例来说明这一点:

#include <functional>
#include <string>

class  Foo
{
private:
    int val1 , val2;
};    

class Bar
{
public:
    void function ( ) {
        //do stuff;
        Foo f;
        function ( f );
    }    
    void function ( const Foo& f ) {
        //do stuff
    }

private:
    //random attribute
    std::string str;    
};


void otherFunction ( std::function<void ( Bar& , const  Foo& ) > function ) {
    Bar b;
    Foo f;
    function(b,f);
} …
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers class c++11 std-function

4
推荐指数
1
解决办法
180
查看次数