468,168 Members | 1,546 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,168 developers. It's quick & easy.

Multiple operator() overloading for functor object

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :

I was trying to implement an arbitrary integer precision class (I know
such libraries already exist) and I wrote the following piece of code
:

template <typename T> class plus_with_carry {
public:
plus_with_carry(void) : carry(false) {};
T operator() (const T& left, const T& right) {
T temp = left + right + ((carry) ? 1 : 0);
carry = (temp<left);
};
T operator() (const T& arg) {
T temp = arg + ((carry) ? 1 : 0);
carry = (temp<arg);
};
bool carry;
};
to be used as a functor in the CBigInt operator+ (not yet complete) :

const CBigInt operator+ (const CBigInt& left, const CBigInt&
right) {
CBigInt temp;
if (left.sign==right.sign) {
std::vector<typename CBigInt::T>::size_type
sizel =
left.values.size();
std::vector<typename CBigInt::T>::size_type
sizer =
right.values.size();
temp.sign=left.sign;
plus_with_carry<typename CBigInt::T> myplus();
if (sizel<sizer) {
temp.values.reserve(sizer);
std::transform(left.values.begin(),
left.values.end(),
right.values.begin(),
std::back_inserter(temp.values), myplus);

std::transform(right.values.begin()+sizel, right.values.end(),

std::back_inserter(temp.values), myplus);
} else {
temp.values.reserve(sizel);
std::transform(right.values.begin(),
right.values.end(),
left.values.begin(),
std::back_inserter(temp.values), myplus);

std::transform(left.values.begin()+sizer, left.values.end(),

std::back_inserter(temp.values), myplus);
}
} else {
}
return temp;
}

but my compiler does not find the binary version of the functor
plus_with_carry...

Is this a problem with my way of writing the functor ? Is this allowed
by the standard ?

Thanks in advance for your help,

Charles
Jul 22 '05 #1
2 2156
Charles-Antoine Giuliani wrote in news:kkauk0p5v6kknovdd4v8669ri6lrd377nl@
4ax.com in comp.lang.c++:

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :


Once I reformated you code (*) I found:

plus_with_carry<typename CBigInt::T> myplus();

This is a function declaration:

myplus a function taking no arguments and returning a
plus_with_carry<typename CBigInt::T>

Loose the parenthesis.

HTH.

*) Most newsreaders wrap at 76 char's, when I got you code
operator + was a complete mess.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Thanks a lot for this fast answer, it does the trick ! Pretty stupid
from me ;-)

This was my first time posting on the newsgroup, so pardon me for the
bad formatting...

Charles

On 20 Sep 2004 19:37:11 GMT, Rob Williscroft <rt*@freenet.co.uk>
wrote:
Charles-Antoine Giuliani wrote in news:kkauk0p5v6kknovdd4v8669ri6lrd377nl@
4ax.com in comp.lang.c++:

Is it possible to overload multiple times operator() ? For example for
using one or two arguments ? My visual c++ does not seem to like it :


Once I reformated you code (*) I found:

plus_with_carry<typename CBigInt::T> myplus();

This is a function declaration:

myplus a function taking no arguments and returning a
plus_with_carry<typename CBigInt::T>

Loose the parenthesis.

HTH.

*) Most newsreaders wrap at 76 char's, when I got you code
operator + was a complete mess.

Rob.


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Malcolm Smith | last post: by
3 posts views Thread by Johan | last post: by
1 post views Thread by sarathy | last post: by
7 posts views Thread by John | last post: by
3 posts views Thread by Eric Lilja | last post: by
2 posts views Thread by aaragon | last post: by
9 posts views Thread by fgh.vbn.rty | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.