By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

static operator() in functor?

P: n/a
Hi everyone,

Can someone point me out why I can't declare the operator() of a
functor as static? The reason behind this is that I want to be able to
call to the function without instantiating the Functor object. The
code is as follows:

#include <iostream>
using namespace std;

class Functor
{
public:

static void operator()(double c, double f){
cout<<"c -"<<c<<endl;
cout<<"f -"<<f<<endl;
}
};

int main()
{
Functor::operator()(4,5);
return 1;
}

The error message is:
aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx:9: error: 'static void Functor::operator()(double, double)'
must be a nonstatic member function

Thank you.

May 27 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
aaragon wrote:
Hi everyone,

Can someone point me out why I can't declare the operator() of a
functor as static?
An operator operates on an instance of a class.

--
Ian Collins.
May 27 '07 #2

P: n/a
On May 27, 4:08 am, aaragon <alejandro.ara...@gmail.comwrote:
Can someone point me out why I can't declare the operator() of a
functor as static?
Because the standard forbids it.

I don't think that there is any underlying technical reason, per
se; it would be easy for the compiler to implement. But what
would the operator syntax be if you didn't have an object; the
purpose of an overloaded operator is to be able to use the
operator syntax. Thus:

struct Toto
{
static void operator()() ; // suppose this were legal...
} ;

Toto aToto ;

aToto() ; // fine...
Toto::operator()() ; // also OK.

// But how would use use the operator syntax without an
// object?

Given this, it makes "logical" sense to require it to be a
non-static member.
The reason behind this is that I want to be able to
call to the function without instantiating the Functor object. The
code is as follows:
#include <iostream>
using namespace std;
class Functor
{
public:
static void operator()(double c, double f){
cout<<"c -"<<c<<endl;
cout<<"f -"<<f<<endl;
}
};
int main()
{
Functor::operator()(4,5);
return 1;
}
The error message is:
aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx:9: error: 'static void Functor::operator()(double, double)'
must be a nonstatic member function
The obvious solution: give the function a name, and call the
named function in your operator()(). The raison d'être of an
operator is to allow use with the operator syntax.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.