如何在类/函数模板中传递 void 参数

发布于 2024-11-26 09:39:32 字数 825 浏览 3 评论 0原文

嘿,我正在尝试创建一个按钮模板类,它是用按钮在按下时收到的(例如鼠标位置)和指向应该调用的函数的指针构造的。

然而,按钮通常返回 void 并且不带任何参数(您按下的按钮会发生一些事情:它们不带任何参数,它们被按下然后只做一些事情。)所以我将如何生成类成员函数,因为显然我不能将 void 作为参数类型?

这是源代码(如果有帮助的话):

    template<typename Return = void, typename Arg1 = void, typename Arg2 = void> 
class Button 
{
private:
    boost::function<Return (Arg1, Arg2)> Function;
    //Return (*Function)(Arg1, Arg2);              // this didn't work so i tried boost::function

public:
    void Activate(Arg1, Arg2){ Function(Arg1, Arg2) ;};

    void SetFunction(Return (*Function)(Arg1, Arg2)){
        this->Function= Function;};

    //constructors
    Button(){ Function= 0;};

    Button( Return (*Function)(Arg1, Arg2)){  
        this->Function = &Function; };
};

Hey i'm trying to make a Button template class, which is constructed with the the button would recieve when pressed (such as mouse position), and a pointer to the function that should be called.

However buttons often return void and take no arguments (Buttons that you press and something happens: they don't take any arguments, they're pressed and then just do something.) so how would i generate the classes member functions since apparently i can't have void as an argument type?

Here's the source if it's helpful:

    template<typename Return = void, typename Arg1 = void, typename Arg2 = void> 
class Button 
{
private:
    boost::function<Return (Arg1, Arg2)> Function;
    //Return (*Function)(Arg1, Arg2);              // this didn't work so i tried boost::function

public:
    void Activate(Arg1, Arg2){ Function(Arg1, Arg2) ;};

    void SetFunction(Return (*Function)(Arg1, Arg2)){
        this->Function= Function;};

    //constructors
    Button(){ Function= 0;};

    Button( Return (*Function)(Arg1, Arg2)){  
        this->Function = &Function; };
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2024-12-03 09:39:32

您可以指定 void 类型的模板规范,例如,您可以使用模板类 button 的以下变体:

template <typename rtnVal, typename Val1, typename Val2>
class Button {
private:
    rtnVal(*Function)( Val1 val1, Val2 val2 );

public:
    Button() : Function( nullptr ) {}

    void SetFunction( rtnVal(*func)(Val1, Val2) ) {
        Function = func;
    }

    rtnVal RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }
};

// Special void type, accepting arguments overload:
template < typename Val1, typename Val2 >
class Button< void, Val1, Val2 > {
private:
    void(*Function)(Val1 val1, Val2 val2);

public:
    Button() : Function( nullptr ) {}

    void SetFunction( void(*func)(Val1, Val2) ) {
        Function = func;
    }

    void RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }

};

// Pure void type:
template<>
class Button<void, void, void> {
private:
    void(*Function)( void );

public:
    Button() : Function( nullptr ) {}

    void SetFunction( void(*func)() ) {
        Function = func;
    }

    void RunFunction() { 
        return Function();
    }
};

然后,这允许您初始化并使用 void 作为参数,例如,给定void function Print() 以下内容现在有效:

void Print()
{
    std::cout << "Function has been called" << std::endl;
}

int main()
{
    Button< void, void, void > btn;

    btn.SetFunction( Print );

    btn.RunFunction();

    std::cout << "Finished";
}

我希望这有助于澄清问题! :)

注意:nullptr 是一个 C++0x 关键字,如果您的编译器尚未实现它,请使用 #define nullptr 0

You can specify a template specification of type void, for example, you could use the following variations of the templated class, button:

template <typename rtnVal, typename Val1, typename Val2>
class Button {
private:
    rtnVal(*Function)( Val1 val1, Val2 val2 );

public:
    Button() : Function( nullptr ) {}

    void SetFunction( rtnVal(*func)(Val1, Val2) ) {
        Function = func;
    }

    rtnVal RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }
};

// Special void type, accepting arguments overload:
template < typename Val1, typename Val2 >
class Button< void, Val1, Val2 > {
private:
    void(*Function)(Val1 val1, Val2 val2);

public:
    Button() : Function( nullptr ) {}

    void SetFunction( void(*func)(Val1, Val2) ) {
        Function = func;
    }

    void RunFunction( Val1 val1, Val2 val2 ) { return Function( val1, val2 ); }

};

// Pure void type:
template<>
class Button<void, void, void> {
private:
    void(*Function)( void );

public:
    Button() : Function( nullptr ) {}

    void SetFunction( void(*func)() ) {
        Function = func;
    }

    void RunFunction() { 
        return Function();
    }
};

This then allows you to initialize and use void as arguments, for example, given a void function Print() the following would now be valid:

void Print()
{
    std::cout << "Function has been called" << std::endl;
}

int main()
{
    Button< void, void, void > btn;

    btn.SetFunction( Print );

    btn.RunFunction();

    std::cout << "Finished";
}

I hope this helps to clear things up! :)

Note: nullptr is a C++0x keyword, if your compiler hasn't implemented it use #define nullptr 0

或十年 2024-12-03 09:39:32

如果我关注你想要拥有它,以便用户可以有 0-2 个参数作为参数?

如果您愿意使用 c++0x 那么您可以使用可变参数模板。这将允许您根据需要使用任意数量的参数。

if i follow you want to have it so the user can have 0-2 args for the params?

if you are willing to use c++0x then you can use a variadic template. This will allow you to use as many or as few args as you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文