question on operator ( ) overloading

X

xuatla

Hi,

I have a class myClass, and want to define operator () with it. But I
don't want to change the class itself (e.g., sometime the class is
defined by other people and I cannot change it), but want to define it
outside the class. Is it possible to do so?

e.g.,

class myClass
{
//.....

data_type operator [] (size_t i){
//...
} ;

} ;

data_type operator () (const myClass& c, size_t i)
{
return c;
}

When I compiled this, I got an error:

error: `data_type operator()(const myClass&, size_t)' must be a
nonstatic member function.

Google gives:
An overloaded `operator ()' must be a non-static member function.

Does it mean that operator () cannot be overloaded out of class?

Thanks.

-X
 
V

Victor Bazarov

xuatla said:
I have a class myClass, and want to define operator () with it. But I
don't want to change the class itself (e.g., sometime the class is defined
by other people and I cannot change it), but want to define it outside the
class. Is it possible to do so?

No.


V
 
D

Daniel T.

xuatla <[email protected]> said:
Hi,

I have a class myClass, and want to define operator () with it. But I
don't want to change the class itself (e.g., sometime the class is
defined by other people and I cannot change it), but want to define it
outside the class. Is it possible to do so?

e.g.,

class myClass
{
//.....

data_type operator [] (size_t i){
//...
} ;

} ;

data_type operator () (const myClass& c, size_t i)
{
return c;
}

When I compiled this, I got an error:

error: `data_type operator()(const myClass&, size_t)' must be a
nonstatic member function.

Google gives:
An overloaded `operator ()' must be a non-static member function.

Does it mean that operator () cannot be overloaded out of class?


Yes. It must be a non-static member function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top