I realize this isn't necessarily something you would want to do, but I ran into some code similar to this the other day. So, I want to look into the idea a bit more...
I have a class that has overloaded a constructor and a functor. This works as expected. However, when you call a class with the default constructor and then try to use the functor, it fails (because it can't figure out how to convert the class to an int, which isn't what I would want, anyhow.) What would be the correct way of doing this?
#include <cstdio>
class foo
{
public:
foo(int a, int b);
foo();
int operator()();
private:
int result;
};
foo::foo(int a, int b)
{
printf("I am a foo(int, int). a: %d b: %d\n", a, b);
result = a + b;
}
foo::foo()
{
printf("I am a base foo!\n");
result = -1;
}
int foo::operator()()
{
return result;
}
int main(void)
{
int ret1 = -12345;
int ret2 = -12345;
foo myfoo(3, 8);
foo otherfoo();
ret1 = myfoo();
ret2 = otherfoo(); //fails. Also, otherfoo()() fails.
printf("foo: %d otherfoo: %d\n", ret1, ret2);
return 0;
}