How do I put a pointer to an operator?

L

leriaat

I would like to create a calculator, and I thought that it would be a
good idea to write a universal "do_it" function that would have a
pointer to the operator and the two operands like this:

double do_it(double a, doulbe b, doulbe (*op)(doulbe, doulbe) ) {
return op(a, b); }

Well, the idea is great, but I don't know how could I put a pointer to
the + operator... Is it possible at all? I mean something like this:

cout << do_it(1, 2, &(operator+) );
 
R

Rolf Magnus

I would like to create a calculator, and I thought that it would be a
good idea to write a universal "do_it" function that would have a
pointer to the operator and the two operands like this:

double do_it(double a, doulbe b, doulbe (*op)(doulbe, doulbe) ) {
return op(a, b); }

Well, the idea is great, but I don't know how could I put a pointer to
the + operator... Is it possible at all? I mean something like this:

cout << do_it(1, 2, &(operator+) );

You can't get pointers to the built-in operators. You can write functions
that themselves use those operators and then get pointers to those
functions. But I think it would be better to make an abstract Operator base
class and for each operation that your calculator can do add a class that
derives from it. Something like:

#include <iostream>

class Operator
{
public:
virtual double operator()(double a, double b) const = 0;
virtual const char* name() const = 0;
};

class Plus : public Operator
{
double operator()(double a, double b) const
{
return a + b;
}
const char* name() const { return "+"; }
};

class Minus : public Operator
{
double operator()(double a, double b) const
{
return a - b;
}
const char* name() const
{
return "-";
}
};

void calc_result(double a, double b, const Operator& op)
{
std::cout << a << ' ' << op.name()
<< ' ' << b << " = "
<< op(a, b) << '\n';
}

int main()
{
double a = 7, b = 6;
calc_result(a, b, Plus());
calc_result(a, b, Minus());
}
 
V

Victor Bazarov

cout said:
You can't get pointers to the built-in operators. You can write
functions that themselves use those operators and then get pointers
to those functions. But I think it would be better to make an
abstract Operator base class and for each operation that your
calculator can do add a class that derives from it. Something like:

#include <iostream>

class Operator
{
[..]
};

class Plus : public Operator
{
[..]
};

class Minus : public Operator
{
[..]
};

void calc_result(double a, double b, const Operator& op)
{
std::cout << a << ' ' << op.name()
<< ' ' << b << " = "
<< op(a, b) << '\n';
}

I always thought that 'std::plus' and 'std::minus' were specifically
for that purpose (except the "name", of course). See 20.3.2, and the
documentation on your library implementation, of course.

V
 
R

Rolf Magnus

Victor said:
cout said:
You can't get pointers to the built-in operators. You can write
functions that themselves use those operators and then get pointers
to those functions. But I think it would be better to make an
abstract Operator base class and for each operation that your
calculator can do add a class that derives from it. Something like:

#include <iostream>

class Operator
{
[..]
};

class Plus : public Operator
{
[..]
};

class Minus : public Operator
{
[..]
};

void calc_result(double a, double b, const Operator& op)
{
std::cout << a << ' ' << op.name()
<< ' ' << b << " = "
<< op(a, b) << '\n';
}

I always thought that 'std::plus' and 'std::minus' were specifically
for that purpose (except the "name", of course). See 20.3.2, and the
documentation on your library implementation, of course.

They are made for generic algorithms and don't provide run-time
polymorphism.
When writing a calculator in C++, you usually waht the user to be able to
specify the operation to be done at run-time.
 
V

Victor Bazarov

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top