Why no static member operator() functions?

Discussion in 'C++' started by Urs Thuermann, Nov 4, 2011.

  1. 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
     
    Urs Thuermann, Nov 4, 2011
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Nov 4, 2011
    #2
    1. Advertising

  3. 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
     
    Marcel Müller, Nov 4, 2011
    #3
  4. 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::eek:stream& operator<<(std::eek: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
     
    Urs Thuermann, Nov 7, 2011
    #4
  5. 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::eek:stream& operator<<(std::eek: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
     
    Victor Bazarov, Nov 7, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,073
    Smokey Grindel
    Dec 2, 2006
  2. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
  3. Hicham Mouline
    Replies:
    0
    Views:
    441
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    423
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    726
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page