473,396 Members | 2,039 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,396 software developers and data experts.

static operator() in functor?

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
2 20122
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
2
by: Charles-Antoine Giuliani | last post by:
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...
6
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write...
8
by: tron.thomas | last post by:
I'm using Microsoft Development Environment 2002 Version 7.0.9466 on Windows XP Professional Service Pack 2. If I compile the following code at maximum warning level: #include <functional> ...
3
by: chsalvia | last post by:
In generic programming, static member functions and functors seem to be very useful. I've noticed that some libraries take the approach of using templated static member functions to provide...
12
by: mathieu | last post by:
Hi, Consider the following (*). Is there a way to rewrite it so that it remains convenient (N is being recomputed when array v is modified) *and* compiles :) Thanks, -Mathieu (*)
2
by: mathieu | last post by:
Hi there, I was recently help for an issue with the following code: template <typename T, unsigned int N> struct Functor { T values; };
2
by: Paul Bilokon | last post by:
Hi all, A very simple question, but I couldn't find a definitive answer in the literature. Functors provide you with the function call syntax: foo(1, 2, 3) may be a call to a functor. Now, what...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.