libcurl回调w / c ++类成员 [英] libcurl callbacks w/c++ class members

查看:206
本文介绍了libcurl回调w / c ++类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取自libcurl网站上的 libcurl编程教程: / p>

Taken from the libcurl programming tutorial on the libcurl site:


libcurl与C ++

libcurl with C++

当接口libcurl时,C ++
而不是C:

There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl:

回调不能是非静态类成员函数

The callbacks CANNOT be non-static class member functions

示例C ++代码:

class AClass 
{   
    static size_t write_data(void *ptr, size_t size, size_t nmemb,   void ourpointer)       
    {   /* do what you want with the data */   }  
}


这个限制是什么,是范围的问题吗?如果每个类成员都有自己的easy_handle,那么它然后可以使用回调它自己的非静态成员函数?它不会说。我可以用这种方式工作,但我很好奇为什么它存在。

What is this limitation for, is it a problem of scope? If each class member has it's own easy_handle then can it then use callbacks to it's own non static member functions? It doesn't go on to say. I can work with this either way, but I'm curious why it exists.

编辑:另一件事;这是否限制从类中创建easy_handle的?这将会对我的设计产生一个问题,如果是的话,我希望每个对象都有自己的easy_handle。

edit: One more thing; does this impose a limitation on creating easy_handle's from within a class? That would greatly pose a problem for my design if so, I'd like each object to have its own easy_handle.

推荐答案

非静态成员函数通常不允许作为C回调,因为它还需要实例指针作为隐藏参数,这是调用C代码不期望的(并且这个隐藏参数,取决于编译器,可能不会传递

A non-static member function is normally not allowed as a C callback because it neeeds also the instance pointer as a hidden parameter, which is not expected by the calling C code (and this hidden parameter, depending from the compiler, may not be passed on the stack, but on a particular register, ...).

静态成员函数通常实现为具有特定范围的普通函数与ABI无关)。

Static member functions, instead, are normally implemented as "normal" functions with a particular scope (which is not relevant as far as the ABI is concerned).

这篇关于libcurl回调w / c ++类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆