简体   繁体   English

运营商可以用作功能吗? (C ++)

[英]Can operators be used as functions? (C++)

This is similar to another question I've asked, but, I've created an expression class that works like so: 这类似于我提出的另一个问题,但是,我创建了一个表达式类,其工作原理如下:

expression<int, int> exp(10, 11, GreaterThan);
//expression<typename T, typename U> exp(T val1, U val2, oper op);
//where oper is a pointer to bool function(T, U)

where GreaterThan is a previously defined function. 其中GreaterThan是先前定义的函数。 And I am wondering why I can't do this: 我想知道为什么我不能这样做:

expression<int, int> exp(10, 11, >);

particularily when > is overloaded as 特别是当>超载时

bool operator>(int a, int a){return (a > b);}

which is identical to GreaterThan: 这与GreaterThan相同:

bool GreaterThan(int a, int b){return (a > b);}

A function that returns bool and takes two arguments. 一个返回bool并接受两个参数的函数。

I'm not sure if this is what you are asking, but the C++ standard library provides function objects for many of C++'s operators, declared in the header <functional> . 我不确定这是不是你要问的,但是C ++标准库为许多C ++的运算符提供了函数对象,在头文件<functional> This includes std::plus (+), std::minus (-), std::multiplies (*), std::divides (/), std::modulus (%), std::negate (unary -), std::equal_to (==), std::not_equal_to (!=), std::less (<), std::greater (>), std::less_equal (<=), std::greater_equal (>=), std::logical_and (&&), std::logical_or (||), std::logical_not (!) 这包括std::plus (+), std::minus ( - ), std::multiplies (*), std::divides (/), std::modulus (%), std::negate (unary - ) , std::equal_to (==), std::not_equal_to (!=), std::less (<), std::greater (>), std::less_equal (<=), std::greater_equal (> =), std::logical_and (&&), std::logical_or (||), std::logical_not (!)

So maybe you just want 所以也许你只是想要

expression<int, int> exp(10, 11, std::greater<int>());

Instead of: 代替:

expression<int, int> exp(10, 11, >);

you could do this: 你可以这样做:

expression<int, int> exp(10, 11, operator>);

You could because it doesn't work for integers. 可以,因为它不适用于整数。 But it will work for other types or operators that you will overload. 但它适用于您将重载的其他类型或运算符。

The operators that you overload are normal functions, so actually you are playing with function pointers. 您重载的运算符是正常函数,因此实际上您正在使用函数指针。

You cannot have written the following: 你不能写下面的内容:

bool operator>(int a, int a){return (a > b);}

C++ does not allow overloading of operators for the built-in types. C ++不允许为内置类型重载运算符。 Please re-write your question, and particularly give the actual code for the expression template. 请重新编写您的问题,特别是给出表达式模板的实际代码。

Neil is right, this wouldn't work for built-in types. 尼尔是对的,这不适用于内置类型。 At least one of the parameters must be "formal class type". 至少有一个参数必须是“正式类型”。 Another problem is that 另一个问题是

expression<int, int> exp(10, 11, >);

is not what compiler likes you to write. 不是编译器喜欢你写的东西。 Instead you can use something like the following code 相反,您可以使用类似下面的代码

class A
{

};
class B
{

};
typedef bool (*oper)(A& a, B& b);
bool operator>(A& a, B& b)
{
  return false;

}
bool func(A&a, B& b, oper op)
{

}

and then in your code 然后在你的代码中

  A a;
  B b;
  func(a, b, operator>);

The + operator for example is of type 例如+运算符是类型的

int (__thiscall Integer::*)(int value)

for the operator function 用于操作员功能

int Integer::operator + (int i)

To pass the operator function you simply do this: 要传递操作员功能,您只需执行以下操作:

Integer a(10), b(20);
add(a, b, &Integer::operator +);

To use it do this: 要用它,请执行以下操作:

(a.*functionPtr)(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM