English 中文(简体)
非STL功能抗拒使用
原标题:non-STL function object callback usage

On the net I found some examples about function objects (callees) but they did not demonstrate how to use them as an argument for a callback function (which calls them, the caller) but merely the way it is used when using sort or for_each. Therefore, I made one myself:

class CBase
{
public:
 virtual int operator()(int a, int b)
 {
  return 10;
 }
};

class CSmaller: public CBase
{
public:
 int operator()(int a, int b)
 {
  return a < b;
 }

 int Compute(int a, int b)
 {
  return a < b;
 }
};

class CLarger: public CBase
{
public:
 int operator()(int a, int b)
 {
  return a > b;
 }

 int Compute(int a, int b)
 {
  return a > b;
 }
};

int caller(CBase &f, int x0, int x1) 
{
 return f(x0,x1);
}

int main()
{
 CLarger callee1; /*this callee is being called by caller*/
 CSmaller callee2; /*idem*/
 int q=caller(callee1,0,1);
 int z=caller(callee2,0,1);
 printf("%d
",q);
 printf("%d
",z);
}

这些STL功能物体(比较少)是否以这种方式得到实施(因为通常只是在使用一般名称/分类模板之前)? I.e.,这些公司(CLarger和CSmall)是否有关系/共同藏匿处,每个类别都有虚拟功能,或者它们是否也属于某些一般功能目标(只要它们执行(有两种论点的)操作者?) 与此类似(未经过测试):

template<class F>
float integrate(const F &f,float x0,float x1) 
{
 ..f(x)..;
}

float derive(const F &f,float x0,float x1) 
{
 ..f(x)..;
}

class CSquare 
{
public:
 float operator()(float x)
 {
  return x*x;
 }
};

class CTwoX 
{
public:
 float operator()(float x)
 {
  return 2*x;
 }
};

如果是正确的,如何以非STL的方式执行这些非相关的课程? 我知道,使用模板维持基本机制(以这种方式与之相关),但只有类型不同。 但是,是否需要这种关系(即,同样的追随者)?

Is using an overloaded () in a function object only meant for convenience? Because I can use CLarger s Compute as well. I can also merge those separated classes (that used virtual for their different functionality) in a class CCompute with members Larger and Smaller, for example. BTW, I think I can put derive and integrate in a class as well. Do my qeustions make sense, or do my thoughts contradict the STL theory?

问题回答

你们可以采取两种方式:继承或模板。 这两项要求都不属要求。 STL的功能是模板式的,这意味着只要表达方式f(x)按要求操作,你就可以将任何内容通过<代码>f(例如,功能点人、轨迹——你称之为被点)。

注:f(x)只是一个例子,参数的确切数目取决于所使用部分STL功能。

继承的好处是,你能够根据实际操作类型,在基类中总结共同行为和执行。

模板的好处是,你可以使用任何类型的模板(基于模板参数)——这样,虽然你能够超负荷使用<代码>operator(>,但是,在每类产品中,如果需要某些类型的特殊处理,则可能更容易建立一个模板类别。

请注意,当你使用模板时,该模板为每个实际的参数组合设定了不同的类型。

STL algorithms which accept a function argument (for example, a predicate) will generally not care how the argument is created so long as it matches a "function like" interface; all it needs to do is deduce the type with template argument deduction along the lines of

template< typename FwdIter, typename Pred >
FwdIter find_if( FwdIter first, FwdIter last, Pred pred );

/* .. etc ... */

bool is_equal_to_one(int n) { return n == 1; }

int main()
{
    std::vector<int> foo;
    std::vector<int>::const_iterator iter;
    iter = std::find_if( foo.begin(), foo.end(), is_equal_to_one );
}

<代码>可能有太多不同的方法。 预设型号,以任何其他方式加以实施(功能、ctor、 st:功能......)。 发现“如果职能依赖<代码>。 Pred 是与bool(*FwdIt)/code>接口相吻合的任何东西。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?