感谢C++ 11,我们收到了std::functionfunctor包装器系列.不幸的是,我一直只听到关于这些新增内容的不好的事情.最受欢迎的是它们非常慢.我对它进行了测试,与模板相比,它们真的很糟糕.
#include <iostream>
#include <functional>
#include <string>
#include <chrono>
template <typename F>
float calc1(F f) { return -1.0f * f(3.3f) + 666.0f; }
float calc2(std::function<float(float)> f) { return -1.0f * f(3.3f) + 666.0f; }
int main() {
using namespace std::chrono;
const auto tp1 = system_clock::now();
for (int i = 0; i < 1e8; ++i) {
calc1([](float arg){ return arg * 0.5f; });
}
const auto tp2 = high_resolution_clock::now();
const auto d = duration_cast<milliseconds>(tp2 - tp1);
std::cout << …Run Code Online (Sandbox Code Playgroud) 我想将一个函数指针设置为一个类的成员,该类是指向同一个类中另一个函数的指针.我这样做的原因很复杂.
在这个例子中,我希望输出为"1"
class A {
public:
int f();
int (*x)();
}
int A::f() {
return 1;
}
int main() {
A a;
a.x = a.f;
printf("%d\n",a.x())
}
Run Code Online (Sandbox Code Playgroud)
但这在编译时失败了.为什么?
我有2个C++类问题:
第一个问题是:我怎么能这样做,所以我可以将类成员函数作为参数传递给另一个函数?如何运行/调用该函数?我怎么能用类静态函数做同样的事情.通过查看此代码可能更容易理解我的问题:
class DebuggingManager
{
string testLog;
bool test1()
{
// run test & return whether it passed or failed
}
static bool test2()
{
}
// How can I call a member function?
void catalogueTest( string testName, bool DebuggingManager::*nMemberFunction )
{
testLog += "Status of " + testName + ": " + ((*)nMemberFunction()) + "\n";
}
// How can I call a static function?
void catalogueTest( string testName, bool DebuggingManager::*nStaticFunction )
{
testLog += "Status of " + testName …Run Code Online (Sandbox Code Playgroud) 我有两个函数,其中一个函数作为一个参数,这工作得很好,但我想在我的第二个函数中调用这个传递的函数.
class XY {
public:
void first(void f());
void second();
};
void XY::first(void f()){
}
void XY::second(){
f(); //passed function from first()
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个包含三个成员函数的类,如下所示:
#include <iostream>
#include <functional>
class ClassName
{
public:
double add(double a, double b);
double intermediate(double a, double b, std::function<double (double,double)> func);
double combiner(double a, double b);
};
double ClassName::add(double a, double b)
{
return a+b;
}
double ClassName::intermediate(double a, double b, std::function<double (double,double)> func)
{
return func(a, b);
}
double ClassName::combiner(double a, double b)
{
return intermediate(a, b, add);
}
int main()
{
ClassName OBJ;
std::cout << OBJ.combiner(12, 10);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是将成员函数"add"传递给成员函数"intermediate",然后由"combiner"调用.但是,我不认为我使用正确的语法,因为当我尝试编译它时,我收到一条错误,说"非标准语法;使用'&'创建指向成员的指针." 我不太确定会出现什么问题,因为如果这些函数不是类中的成员函数(只是命名空间中定义的常规函数),则此方法可以正常工作. 那么是否可以将成员函数传递给另一个成员函数?
我试图将任何泛型函数作为 C++ 函数中的参数传递。作为额外的乐趣,该函数将驻留在其自己的单独类中。在这种特殊情况下,它们都将不带任何参数并返回void。这包括任何泛型类的成员。
我当前的代码如下所示:
class functionsFedHere {
public:
void (*callback)();
functionsFedHere(void(*)());
}
functionsFedHere::functionsFedHere (void(*callbackFunction)()) {
callback = callbackFunction;
}
void fn1() { cout<<"Fn1"<<endl; }
class myClass {
public:
int i;
void fn2() { cout<<i<<endl; }
}
class myOtherClass {
public:
string j;
void fn3() { cout<<j<<endl; }
}
int main() {
// Initialise some objects
myClass b;
b.i = 2;
myOtherClass c;
c.j = "some text";
// Works fine with non-member functions
functionsFedHere objectA(fn1);
objectA.callback();
// Doesn't work …Run Code Online (Sandbox Code Playgroud) 我在尝试将函数作为参数传递给另一个对象的函数时遇到问题.我很清楚有很多类似的主题,但我要么无法让他们的解决方案工作或无法理解它们.
class foo
{
public:
void func1(void (*Drawing)(void));
template<class T>
void func2(void (T::*Drawing)(void));
};
class bar
{
private:
foo myFoo;
void Drawing();
void func3() {
// Attempt 1
myFoo.func1(Drawing);
// Attempt 2
myFoo.func2<bar>(&bar::Drawing);
}
};
Run Code Online (Sandbox Code Playgroud)
所以在我的第一次尝试中,我得到的错误是你无法转换void (bar::*)(void)为void (*)(void)我发现有正常的函数指针和成员函数指针.
尝试2是我努力克服这个问题,但我现在得到了未解决的外部因素......
那么如何成功将我的Drawing()成员函数从另一个对象传递到另一个函数呢?
c++ member-function-pointers function-pointers member-functions
我在 C++ 中将函数作为参数传递时遇到问题。我在这里重新创建了一个最小的工作示例:
#include <iostream>
using namespace std;
void print(const double &func(double)) {
cout << func(1) << endl;
}
class Obj {
public:
double MyFunction(double x);
void PrintFunction();
};
void Obj::PrintFunction(){
print(MyFunction);
}
double Obj::MyFunction(double x) {
return x + 1;
}
int main() {
Obj object;
object.PrintFunction();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,整体结构看起来很奇怪,但在完整的代码中更有意义(具体来说,我理想地寻找一个不涉及重组函数所有权的解决方案)。编译时出现错误error: invalid use of non-static member function double Obj::MyFunction(double),这表明我的调用方式有问题print(MyFunction)。我粗略的理解是MyFunction不能单独调用,但是没有包含多少指针、引用等,都成功地得到了代码进行编译。我应该如何正确打电话MyFunction?