这是问题所在:
1)我有一个这样的课:
class some_class
{
public:
some_type some_value;
int some_function(double *a, double *b, int c, int d, void *e);
};
Run Code Online (Sandbox Code Playgroud)
2)在里面some_function,我使用some_values从some_class对象得到一个结果.
3)所以,我有一个具体的对象,我想得到一个指向这个对象的指针some_function.
可能吗?我无法使用,some_fcn_ptr因为此函数的结果取决于some_value对象的具体情况.
如何获得指向some_function对象的指针?谢谢.
typedef int (Some_class::*some_fcn_ptr)(double*, double*, int, int, void*);
Run Code Online (Sandbox Code Playgroud) 在我的C++程序中,我需要调用这个c API:
GConn* gnet_conn_new (const gchar *hostname,
gint port,
GConnFunc func);
Run Code Online (Sandbox Code Playgroud)
其中GConnFunc定义为:
void (*GConnFunc) (GConn *conn);
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我有一个C++类,并有一个成员函数,如:
Class A {
public:
A();
void my_func (GConn* conn);
}
Run Code Online (Sandbox Code Playgroud)
在我的A::A()构造函数,我怎么能传递this->myfunc到gnet_conn_new作为GConnFunc参数?
谢谢.
我有一个可能非常简单的问题:在类中传递并调用成员函数.我知道我想使用BOOST绑定(和/或函数),但我还没有真正掌握它的概念.
以下代码编译并执行问题.但是当我想将"f3"函数更改为非静态类函数时,乐趣就开始了:
#include <iostream>
#include <inttypes.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class Test
{
public:
void f1();
private:
void f2(void (*callfunc)(uint32_t));
static void f3(uint32_t x);
};
void Test::f1(){
f2(f3);
}
void Test::f2(void (*callfunc)(uint32_t)){
(*callfunc)(42);
}
void Test::f3(uint32_t x){
std::cout << "x: " << x << std::endl;
}
int main(int argc, char ** argv)
{
Test ct;
ct.f1();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,改变之后
static void f3(uint32_t x);
Run Code Online (Sandbox Code Playgroud)
至
void f3(uint32_t x);
Run Code Online (Sandbox Code Playgroud)
编译器不满意并告诉我"错误:没有匹配函数调用'Test :: f2()'"
阅读了一些关于boost :: bind和boost :: function的SO帖子,我想我需要改变f2()的定义以及f1()如何调用f2()给f3()作为调用目标,但是除此之外......关于boost :: bind和boost函数的每个组合,我都试过很难编译.
我该如何写这个?作为一个额外的问题:是否有关于boost :: …
我试图将成员函数作为函数指针传递,这样我就不必依赖单例或全局函数来处理Qt 5中的Qt消息。据我所知,std :: function是正确的类型,它具有正确的签名,并且bind应该允许我阻塞隐式this指针,本质上是将成员函数作为全局/非拥有函数传递出去。
void ProgramMessageHandler::setAsMessageHandlerForProgram() {
std::function<void(QtMsgType, const QMessageLogContext &, const QString &)> funcPtr;
funcPtr = std::bind(handleMessages, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
qInstallMessageHandler(funkPtr);
}
Run Code Online (Sandbox Code Playgroud)
这不会编译。我可以成功创建funcPtr变量,但是将其传递给qInstallMessageHandler函数会导致以下情况:
log\ProgramMessageManager.cpp:16: error: cannot convert 'std::function<void(QtMsgType, const QMessageLogContext&, const QString&)>' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}' for argument '1' to 'void (* qInstallMessageHandler(QtMessageHandler))(QtMsgType, const QMessageLogContext&, const QString&)'
oldHandler = qInstallMessageHandler(hackedPointerToHandleFunction);
^
Run Code Online (Sandbox Code Playgroud)
我读过了:
使用std :: bind时从std :: function获取函数指针
但没有人帮助我。
编辑:
因此,有人说没有单例或全局功能是不可能的……但是为什么呢?具有相同签名的成员函数和全局函数之间的唯一区别是它们没有相同的签名,存在隐式this指针作为成员函数的第一个参数。 …
我对C ++有点生疏,经过一年的python转换。自然,我想将python的懒惰翻译成C ++。
我刚刚发现rot13,我对此感到非常兴奋。我找到了3种方法,并且我想做一点性能测试。我还想看看在适当位置修改字符串或创建新字符串是否有区别。所以我最终有6个功能。
在第一种方法中,我使用std :: map映射字符,因此,我建立了一个初始化映射的类,在第二种方法中,我使用了三元运算符,第三种方法是使用了位移。
现在函数原型看起来像这样
// map dependent
void Rot13::convert_inplace(string& mystr){
string Rot13::convert(const string& mystr){
// ternary operator
void bitrot_inplace(string& mystr){
string bitrot(const string& mystr){
// bit shift
void bitshiftrot_inplace(string& mystr){
string bitshiftrot(const string& mystr){
Run Code Online (Sandbox Code Playgroud)
我想构造一个接受这些函数作为参数的函数,然后计算时间并打印结果
typedef void (*vfc)(string str);
void printtime_inplace(string title, vfc func){
Run Code Online (Sandbox Code Playgroud)
我尝试了这种构造,但这意味着我受到vfc返回类型(在我的情况下为voidor)的限制string,并且受制于我需要传递类的指针的事实。因此,我将必须做3个函数来容纳不同的函数,即用于类成员函数的函数,用于void返回类型的函数和用于字符串返回类型的函数。
所以我问自己,是否真的需要使用模板而不编写相同功能的3倍?我对模板真的没有信心,但是我应该做3 typedefs并构造printtime函数以接受模板吗?此外,有没有办法告诉模板您将只接受这些类型(即我定义的类型)?
另一个问题,这可以说是一个好的设计吗?还是您会建议其他设计?其他实现?
试图优化fun_a1()功能.变量j的范围不会改变fun_a1().因此,对于每个'i'迭代检查j == 1或2或3显然是浪费CPU周期.但是如果我尝试将条件评估带到循环之外,我必须为每个条件编写冗余循环.在C中,我可以通过使用函数指针轻松解决这个问题.但是,C++不允许指向非静态函数.我找到了一些描述神秘的"指向成员指针"的链接.(示例1,示例2)但是我仍然不清楚如何从对象本身内部使用它,例如从fun_a()里面?或者它可以以任何其他方式进行优化?
class A{
void fun_b(int i);
void fun_c(int i);
void fun_d(int i);
void fun_f(int i);
void fun_a1(int j){
for(int i=0; i<1000; i++){
if(j==1) fun_b(i);
else if(j==2) fun_c(i);
else if(j==3) fun_d(i);
fun_f(i);
}
}
void fun_a2(int j){
if(j==1){
for(int i=0; i<1000; i++) {
fun_b(i);
fun_f(i);
}
}
else if(j==2){
for(int i=0; i<1000; i++) {
fun_c(i);
fun_f(i);
}
}
else if(j==3){
for(int i=0; i<1000; i++) {
fun_d(i);
fun_f(i);
}
} …Run Code Online (Sandbox Code Playgroud)