473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I put a pointer to an operator?

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+) );

May 3 '06 #1
5 1759
le*****@elte.hu wrote:
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());
}

May 3 '06 #2
Rolf Magnus wrote:
le*****@elte.hu wrote:
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+) );

cout << do_it( 1, 2, std::plus<int>() );
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 3 '06 #3
Victor Bazarov wrote:
Rolf Magnus wrote:
le*****@elte.hu wrote:
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+) );


cout << do_it( 1, 2, std::plus<int>() );
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.

May 3 '06 #4
std::plus<int>() --- Now that's what I was searching for! Thx a lot!

Anyway, where can I find these functions? I mean that I haven't found
it on
http://www.cppreference.com/all_cpp_functions.html

Is there any reference for these "hidden" functtions? Oh, and what's
this 20.3.2 ??

May 3 '06 #5
le*****@elte.hu wrote:
std::plus<int>() --- Now that's what I was searching for! Thx a lot!

Anyway, where can I find these functions? I mean that I haven't found
it on
http://www.cppreference.com/all_cpp_functions.html
I don't use web. I use books. I recommend "C++ Standard Library" by
Josuttis.
Is there any reference for these "hidden" functtions?
In the book I named.
Oh, and what's
this 20.3.2 ??


That's a reference to a subclause of the Standard.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.