通用选择器 [英] universal selector
问题描述
您好!
我想根据给定的索引选择一个可能的实体:
select(index, A,B)
如果指数为0,则
将返回A,如果指数为(或大于),则返回B.
所以我写:
模板< typename T> T select(int index,T a,T b)
{
return(index == 0)? a:b;
}
这在调用所选函数时工作正常:
select(index,somefunction0 ,somefunction1)(args ......)
但是对于班级成员函数失败 - 我该怎么办?
理想情况下我希望它是通用的,不需要
为每个班级重写select()。
-
弗拉基米尔
Hello!
I''d like to select one of possible entities according to a given index:
select(index, A, B)
would return A if index is 0, B if index is (or greater than) 1.
so i write:
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
this works fine when calling selected function:
select(index, somefunction0, somefunction1)(args...)
but fails for class member functions - what should I do?
Ideally I want it universal, without the need
to rewrite select() for each class.
--
Vladimir
推荐答案
2004年11月26日07:45:30 -0800, vl *** ****** @yahoo.com (弗拉基米尔)写道:
On 26 Nov 2004 07:45:30 -0800, vl*********@yahoo.com (Vladimir) wrote:
你好!
我想选择其中一个根据给定指数的可能实体:
select(index,A,B)
如果index为0,则返回A,如果index为(或大于),则返回B 。
所以我写道:
模板< typename T> T select(int index,T a,T b)
返回(index == 0)? a:b;
}
这在调用所选函数时工作正常:
select(index,somefunction0,somefunction1)(args ...)
但是对于班级成员的功能却失败了 - 我该怎么办?
理想情况下我希望它是通用的,而不需要为每个班级重写select()。
Hello!
I''d like to select one of possible entities according to a given index:
select(index, A, B)
would return A if index is 0, B if index is (or greater than) 1.
so i write:
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
this works fine when calling selected function:
select(index, somefunction0, somefunction1)(args...)
but fails for class member functions - what should I do?
Ideally I want it universal, without the need
to rewrite select() for each class.
没有对象你不能调用非静态成员函数。
-
Bob Hairgrove
< a href =mailto:No ********** @ Home.com>没有********** @ Home.com
*弗拉基米尔:
你好!
我想根据给定的索引选择一个可能的实体:
select(index,A,B)
如果index为0,则返回A,如果index为(或大于),则返回B.
所以我写道:
模板< typename T> T select(int index,T a,T b)
返回(index == 0)? a:b;
}
这在调用所选函数时工作正常:
select(index,somefunction0,somefunction1)(args ...)
但是对于班级成员的功能却失败了 - 我该怎么办?
理想情况下我希望它是通用的,而不需要为每个班级重写select()。
Hello!
I''d like to select one of possible entities according to a given index:
select(index, A, B)
would return A if index is 0, B if index is (or greater than) 1.
so i write:
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
this works fine when calling selected function:
select(index, somefunction0, somefunction1)(args...)
but fails for class member functions - what should I do?
Ideally I want it universal, without the need
to rewrite select() for each class.
#include< iostream>
#include" generalmaker.h"
#include< string>
#include< list>
使用命名空间std;
#include< iostream>
>
模板< typename T> T select(int index,T a,T b)
{
return(index == 0)? a:b;
}
class X
{
public:
void a(char const s [])const
{
std :: cout<< a: << s<< std :: endl;
}
void b(char const s [])const
{
std :: cout<< b: << s<< std :: endl;
}
};
int main()
{
X o;
(o。* select(0,X :: a,X :: b))(愚蠢的语法,是的。 ;);
}
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet上最烦人的事情是什么?在电子邮件中?
#include <iostream>
#include "generalmaker.h"
#include <string>
#include <list>
using namespace std;
#include <iostream>
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
class X
{
public:
void a( char const s[] ) const
{
std::cout << "a: " << s << std::endl;
}
void b( char const s[] ) const
{
std::cout << "b: " << s << std::endl;
}
};
int main()
{
X o;
(o.*select( 0, X::a, X::b ))( "Silly syntax, yes." );
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
弗拉基米尔写道:
你好!
我想选择其中一个根据给定指数的可能实体:
select(index,A,B)
如果index为0,则返回A,如果index为(或大于),则返回B 。
所以我写道:
模板< typename T> T select(int index,T a,T b)
返回(index == 0)? a:b;
}
这在调用所选函数时工作正常:
select(index,somefunction0,somefunction1)(args ...)
但是对于班级成员的功能却失败了 - 我该怎么办?
理想情况下我希望它是通用的,而不需要为每个班级重写select()。
Hello!
I''d like to select one of possible entities according to a given index:
select(index, A, B)
would return A if index is 0, B if index is (or greater than) 1.
so i write:
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
this works fine when calling selected function:
select(index, somefunction0, somefunction1)(args...)
but fails for class member functions - what should I do?
Ideally I want it universal, without the need
to rewrite select() for each class.
以下代码编译正常。你的问题究竟是什么?
模板< typename T> T select(int index,T a,T b)
{
return(index == 0)? a:b;
}
struct X
{
void f1(){}
void f2(){}
};
int main()
{
void(X :: * z)()= select(0,& X :: f1,& X :: f2);
X q;
(q。*(选择(1,& X :: f1,& X :: f2)))();
(q。* z)();
}
The code below compiled fine. What exactly is your problem ?
template<typename T> T select(int index, T a, T b)
{
return (index == 0) ? a : b;
}
struct X
{
void f1() {}
void f2() {}
};
int main()
{
void (X::*z)() = select( 0, &X::f1, &X::f2 );
X q;
(q.*(select( 1, &X::f1, &X::f2 )))();
(q.*z)();
}
这篇关于通用选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!