带有仿函数对象的外部功能? [英] external functionality with a functor object?

查看:55
本文介绍了带有仿函数对象的外部功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试通过用户定义的

仿函数对象为类提供一些外部功能。概念如下:


模板< class Functor>

class ClassA

{

...

double evaluate(){

Functor functor_;

return functor_();

}

...

};


//由uesr定义

struct Functor

{

双算子()(){

返回2 + 2;

}

};


int main(int argc,char * argv [])

{

ClassA< Functora;

a.evaluate();

}


我遇到的问题是仿函数需要知道

ClassA可以工作。所以我认为可能使用模板化仿函数

可能有效:

模板< class T>

struct Functor

{

double operator()(T t _){

return t_.x()+ t_.y();

}

};

这是一个好主意,除了现在我不知道

用户将如何分解classA的对象= /

任何想法????

Hi everyone,

I''m trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized functor
may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}
};
This was a good idea except for the fact that now I have no idea of how
the user is going to delacre an object of classA =/
Any ideas????

推荐答案

aaragon写道:
aaragon wrote:

我正试图通过用户定义的

仿函数对象为类提供一些外部功能。概念如下:


模板< class Functor>

class ClassA

{

...

double evaluate(){

Functor functor_;

return functor_();
I''m trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();



或者,在一行而不是两行:


返回Functor()();


:-)

Or, in one line instead of two:

return Functor()();

:-)


}

...

};


//由uesr定义

struct Functor

{

double operator()(){

返回2 + 2;

}

};


int main(int argc,char * argv [])

{

ClassA< Functora;

a.evaluate();

}


我遇到的问题是,仿函数需要知道

ClassA的工作原理。所以我认为可能使用模板化的

仿函数可能有效:

模板< class T>

struct Functor

{

double operator()(T t _){

return t_.x()+ t_.y();

}
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized
functor may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}



如何让''operator()''成为模板?


struct Functor

{

模板< class Tdouble operator()(T t_){

返回t_.x()+ t_.y();

}

};

How about making your ''operator()'' a template?

struct Functor
{
template<class Tdouble operator()(T t_) {
return t_.x() + t_.y();
}
};


};

这是一个好主意,除了现在的事实我不知道

用户怎么去delacre classA的对象A = /

任何想法????
};
This was a good idea except for the fact that now I have no idea of
how the user is going to delacre an object of classA =/
Any ideas????



见上文。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问

See above.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



aaragon写道:

aaragon wrote:

大家好,


我是尝试通过用户定义的

仿函数对象为类提供一些外部功能。概念如下:


模板< class Functor>

class ClassA

{

...

double evaluate(){

Functor functor_;

return functor_();

}

...

};


//由uesr定义

struct Functor

{

双算子()(){

返回2 + 2;

}

};


int main(int argc,char * argv [])

{

ClassA< Functora;

a.evaluate();

}


我遇到的问题是仿函数需要知道

ClassA可以工作。所以我认为可能使用模板化仿函数

可能有效:

模板< class T>

struct Functor

{

double operator()(T t _){

return t_.x()+ t_.y();

}

};

这是一个好主意,除了现在我不知道

用户将如何分解classA的对象= /

任何想法????
Hi everyone,

I''m trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized functor
may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}
};
This was a good idea except for the fact that now I have no idea of how
the user is going to delacre an object of classA =/
Any ideas????



不知道你想用模板Functor做什么,但这个

应该有效。


struct eval

{

int x(){

返回1;

}

int y(){

返回2;

}


};


模板< class Functor>

class ClassA

{

public:

eval tmp;

double evaluate(){

Functor functor_;

return functor_(tmp);

} < br $>
};

模板< class T>

struct Functor

{

double operator()(T t _){

return t_.x()+ t_.y();

}


};


int main(int argc,char * argv [])

{


ClassA< Functor< eval a;

a.evaluate();

}

Not sure what you are trying to do by that Template Functor but this
should work.

struct eval
{
int x(){
return 1;
}
int y(){
return 2;
}

};

template <class Functor>
class ClassA
{
public:
eval tmp;
double evaluate(){
Functor functor_;
return functor_(tmp);
}
};
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}

};

int main(int argc, char* argv[])
{

ClassA<Functor<eval a;
a.evaluate();
}




Victor Bazarov写道:

Victor Bazarov wrote:

aarag on写道:
aaragon wrote:

我正试图通过用户定义的

仿函数对象为类提供一些外部功能。概念如下:


模板< class Functor>

class ClassA

{

...

double evaluate(){

Functor functor_;

return functor_();
I''m trying to provide some external functionality to a class through a
functor object defined by the user. The concept is as follows:

template <class Functor>
class ClassA
{
...
double evaluate(){
Functor functor_;
return functor_();



或者,在一行而不是两行:


返回Functor()();


:-)


Or, in one line instead of two:

return Functor()();

:-)


}

...

};


//由uesr定义

struct Functor

{

double operator()(){

返回2 + 2;

}

};


int main(int argc,char * argv [])

{

ClassA< Functora;

a.evaluate();

}


我遇到的问题是,仿函数需要知道

ClassA的工作原理。所以我认为可能使用模板化的

仿函数可能有效:

模板< class T>

struct Functor

{

double operator()(T t _){

return t_.x()+ t_.y();

}
}
...
};

// defined by the uesr
struct Functor
{
double operator() (){
return 2+2;
}
};

int main(int argc, char *argv[])
{
ClassA<Functora;
a.evaluate();
}

The problem that I have is that the functor needs to know what the
ClassA is to work. So I thought that maybe using a templatized
functor may work:
template <class T>
struct Functor
{
double operator() (T t_){
return t_.x() + t_.y();
}



如何让''operator()''成为模板?


struct Functor

{

模板< class Tdouble operator()(T t_){

返回t_.x()+ t_.y();

}

};


How about making your ''operator()'' a template?

struct Functor
{
template<class Tdouble operator()(T t_) {
return t_.x() + t_.y();
}
};



是的!这很好用! =)

Yes! This works fine! =)


};

这是一个好主意,除了现在我没有

的想法用户怎么去delacre classA的对象A = /

任何想法????
};
This was a good idea except for the fact that now I have no idea of
how the user is going to delacre an object of classA =/
Any ideas????



见上文。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问


See above.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



但是,我还有一个问题。用户需要实现

整个仿函数,我不认为这很好。也许如果我写一个

隐藏的仿函数实现并且让用户只用

覆盖函数()(),也许???

However, I still have one problem. The user needs to implement the
entire functor and I don''t think that is nice. Maybe if I write a
hidden implementation of the functor and the let the user only to
override the function ()(), perhaps???


这篇关于带有仿函数对象的外部功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆