gpt4 book ai didi

c++ - 如何将参数绑定(bind)到构造函数?

转载 作者:行者123 更新时间:2023-12-01 19:07:05 24 4
gpt4 key购买 nike

如上所述,我想使用 std::bind 创建一个函数,该函数在调用时返回一个使用带有默认参数的构造函数构建的对象,如下所示:

#include <functional>

class X
{
int x_, y_;
public:
X(int x, int y): x_(x), y_(y)
{
}
};

int main() {
auto fun = std::bind(&X::X, 1, 2);
X x = fun();
}

相反,我收到以下编译器错误:

error: qualified reference to 'X' is a constructor name rather than a type in this context

error: expected '(' for function-style cast or type construction In reference to this line:

auto fun = std::bind(&X::X, 1, 2);

最佳答案

评论回答了这个问题。显然 std::bind 不能与构造函数和析构函数一起使用,因为它们无法获取其地址。感谢尤金、The Philomath 和 molbdnilo。

关于c++ - 如何将参数绑定(bind)到构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com