Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why no static member operator() functions?

Reply
Thread Tools

Why no static member operator() functions?

 
 
Urs Thuermann
Guest
Posts: n/a
 
      11-04-2011
I tried overloading the operator<<() for ostream as a static member
function in a class, but the compiler (gcc) complained that it should
be non-static or non-member. Why is this not possible? Since I want
to pass a private type of the class as argument, this is a problem.

What I tried to do is something like this:

#include <iostream>

using namespace std;

class Foo {
public:
void foo() {
#if 0
cout << "yadda yadda " << b << endl;
#endif
}
private:
struct Bar {
public:
#if 1
static ostream &operator<<(ostream &os, const Bar &b)
{
b.print(os);
return os;
}
#endif
private:
int bar;
void print(ostream &os) const { os << bar; }
};

Bar b;
};

I cannot make the operator<< a non-member function since its parameter
type Bar is private and I'd prefer to leave it so. How should I solve
this?

When I replace the "operator<<" by some other function name the code
compiles but I still cannot use << like in foo().

urs
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-04-2011
On 11/4/2011 10:01 AM, Urs Thuermann wrote:
> I tried overloading the operator<<() for ostream as a static member
> function in a class, but the compiler (gcc) complained that it should
> be non-static or non-member. Why is this not possible?


Because that's how operators are required to be defined: either
non-static members or non-members.

> Since I want
> to pass a private type of the class as argument, this is a problem.
>
> What I tried to do is something like this:
>
> #include<iostream>
>
> using namespace std;
>
> class Foo {
> public:
> void foo() {
> #if 0
> cout<< "yadda yadda "<< b<< endl;
> #endif
> }
> private:
> struct Bar {
> public:
> #if 1
> static ostream&operator<<(ostream&os, const Bar&b)


Try replacing the keyword 'static' with 'friend'...

> {
> b.print(os);
> return os;
> }
> #endif
> private:
> int bar;
> void print(ostream&os) const { os<< bar; }
> };
>
> Bar b;
> };
>
> I cannot make the operator<< a non-member function since its parameter
> type Bar is private and I'd prefer to leave it so. How should I solve
> this?


Define a non-member operator << and make it call your static member
function (if that's what you need to access that static data member).
The alternative is to declare that non-member operator<< a friend.

> When I replace the "operator<<" by some other function name the code
> compiles but I still cannot use<< like in foo().


So, combine the two...

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      11-04-2011
On 04.11.2011 15:01, Urs Thuermann wrote:
> I tried overloading the operator<<() for ostream as a static member
> function in a class, but the compiler (gcc) complained that it should
> be non-static or non-member. Why is this not possible? Since I want
> to pass a private type of the class as argument, this is a problem.


> class Foo {
> struct Bar {
> public:
> static ostream&operator<<(ostream&os, const Bar&b)
> {
> b.print(os);
> return os;
> }


> I cannot make the operator<< a non-member function since its parameter
> type Bar is private and I'd prefer to leave it so. How should I solve
> this?


What's wrong with
friend ostream&operator<<(ostream&os, const Bar&b);
inside class Foo?


Marcel
 
Reply With Quote
 
Urs Thuermann
Guest
Posts: n/a
 
      11-07-2011
Marcel Müller <> writes:

> On 04.11.2011 15:01, Urs Thuermann wrote:
> > I tried overloading the operator<<() for ostream as a static member
> > function in a class, but the compiler (gcc) complained that it should
> > be non-static or non-member. Why is this not possible? Since I want
> > to pass a private type of the class as argument, this is a problem.

>
> > class Foo {
> > struct Bar {
> > public:
> > static ostream&operator<<(ostream&os, const Bar&b)
> > {
> > b.print(os);
> > return os;
> > }

>
> > I cannot make the operator<< a non-member function since its parameter
> > type Bar is private and I'd prefer to leave it so. How should I solve
> > this?

>
> What's wrong with
> friend ostream&operator<<(ostream&os, const Bar&b);
> inside class Foo?


I am surprised that that works, because I have tried it before I
posted and gcc reported an error since the type Foo::Bar is private
and unusable outside class Foo. OK, I must have done something wrong,
since it now works, both, inline and outside of the class.

But now I've got the next question: How can I make that non-member
friend function static in foo.cc so it's not visible in other
translation units? (I know I can't use it anyway because I cannot
have object of type Foo::Bar, but I'd still prefer to have that
function staitc.)

When I try

friend static ostream &operator<<(ostream &os, const Bar &b);

in class Foo and

static ostream &operator<<(ostream &os, const Foo::Bar &b)
{
b.print(os);
return os;
}

outside any class in foo.cc I get the compiler message

error: storage class specifiers invalid in friend function declarations

and when I remove the static from the friend declaration I get

error: 'std:stream& operator<<(std:stream&, const Foo::Bar&)' was declared 'extern' and later 'static'

Without static in both places, everylooks looks fine.
But is there a way to have that operator<<() static?

urs
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-07-2011
On 11/7/2011 2:19 AM, Urs Thuermann wrote:
> Marcel Müller<> writes:
>
>> On 04.11.2011 15:01, Urs Thuermann wrote:
>>> I tried overloading the operator<<() for ostream as a static member
>>> function in a class, but the compiler (gcc) complained that it should
>>> be non-static or non-member. Why is this not possible? Since I want
>>> to pass a private type of the class as argument, this is a problem.

>>
>>> class Foo {
>>> struct Bar {
>>> public:
>>> static ostream&operator<<(ostream&os, const Bar&b)
>>> {
>>> b.print(os);
>>> return os;
>>> }

>>
>>> I cannot make the operator<< a non-member function since its parameter
>>> type Bar is private and I'd prefer to leave it so. How should I solve
>>> this?

>>
>> What's wrong with
>> friend ostream&operator<<(ostream&os, const Bar&b);
>> inside class Foo?

>
> I am surprised that that works, because I have tried it before I
> posted and gcc reported an error since the type Foo::Bar is private
> and unusable outside class Foo. OK, I must have done something wrong,
> since it now works, both, inline and outside of the class.
>
> But now I've got the next question: How can I make that non-member
> friend function static in foo.cc so it's not visible in other
> translation units? (I know I can't use it anyway because I cannot
> have object of type Foo::Bar, but I'd still prefer to have that
> function staitc.)
>
> When I try
>
> friend static ostream&operator<<(ostream&os, const Bar&b);


Drop the 'static'.

>
> in class Foo and
>
> static ostream&operator<<(ostream&os, const Foo::Bar&b)


Drop the 'static'.

> {
> b.print(os);
> return os;
> }
>
> outside any class in foo.cc I get the compiler message
>
> error: storage class specifiers invalid in friend function declarations


That's what your compiler is telling you: drop the 'static'.

> and when I remove the static from the friend declaration I get
>
> error: 'std:stream& operator<<(std:stream&, const Foo::Bar&)' was declared 'extern' and later 'static'


Huh?

> Without static in both places, everylooks looks fine.


So, what's the problem?

> But is there a way to have that operator<<() static?


WTF for?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
Can't access static member var from static method. Why? Markus Dehmann C++ 5 07-01-2004 04:16 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57