protected member class and operator overloading

Discussion in 'C++' started by Thomas, Sep 20, 2004.

  1. Thomas

    Thomas Guest

    Hi everybody!
    I have the following code:

    ---snip---

    class BaseRecord
    {
    protected:
    struct tFieldLenData
    {
    std::eek:stream &print(ostream &os) const
    {
    os << " name = " <<_name << endl;
    return os:
    }

    // ... do anything ...

    string _name;
    };

    public:
    // ... do anything ...
    };


    std::eek:stream &
    operator << (std::eek:stream &os,
    BaseRecord const &rec);

    std::eek:stream &
    operator << (std::eek:stream &os,
    BaseRecord::tFieldLenData const &fd);

    ---snap---

    From gcc-2.96 upwards I always get the following error message:

    In function `ostream &operator<< (ostream &, const
    BaseRecord::tFieldLenData &)':
    struct BaseRecord::tFieldLenData is protected

    I'm confused about this error, because even the type is declared as
    protected, I expect it to be usable as type within a typedef or
    function declaration outside the scope of it's outer class.
    The fact, that an object of type tFieldLenData cannot be instantiated
    outside the class BaseRecord is clear, but this should not care the
    compiler in this case.
    My idea is, to have the possibilty to print the object to stdout or
    stderr from within the class itself, for instance for debugging
    purpose.

    I now have two questions concerning this point:
    1.) Is this error message appropriate ?
    2.) If you say yes to no. 1, then what would be a nicer solution for
    this case in your opinion ?


    kind regards,
    Thomas
     
    Thomas, Sep 20, 2004
    #1
    1. Advertising

  2. (Thomas) writes:

    > Hi everybody!
    > I have the following code:
    >


    Code:
    [color=blue]
    > From gcc-2.96 upwards I always get the following error message:
    >
    > In function `ostream &operator<< (ostream &, const
    > BaseRecord::tFieldLenData &)':
    > struct BaseRecord::tFieldLenData is protected
    >
    > I'm confused about this error, because even the type is declared as
    > protected, I expect it to be usable as type within a typedef or
    > function declaration outside the scope of it's outer class.
    > The fact, that an object of type tFieldLenData cannot be instantiated
    > outside the class BaseRecord is clear, but this should not care the
    > compiler in this case.[/color]
    
    Why not? To be able to call this operator you need an instace of
    BaseRecord::tFieldLenData, but this type is protected.
    [color=blue]
    > My idea is, to have the possibilty to print the object to stdout or
    > stderr from within the class itself, for instance for debugging
    > purpose.[/color]
    
    Ok, try to make the two operator << function freinds of BaseRecord, that
    only works is you only call this operator inside the class.
    [color=blue]
    > I now have two questions concerning this point:
    > 1.) Is this error message appropriate ?[/color]
    
    IMHO yes.
    [color=blue]
    > 2.) If you say yes to no. 1, then what would be a nicer solution for
    > this case in your opinion ?[/color]
    
    As said, try to make the two << operators friends of BaseRecord.
    
    Kind regards,
    Nicolas
    
    --
    |     Nicolas Pavlidis       |       Elvis Presly:    |\ |__      |
    |   Student of SE  & KM      |      "Into the goto"   | \|__|     |
    |       |       ICQ #320057056      |        |
    |-------------------University of Technology, Graz----------------|
     
    Nicolas Pavlidis, Sep 20, 2004
    #2
    1. Advertising

  3. Thomas

    Thomas Guest

    Hello Nicolas,
    yes, I could solve it with a friend declaration of the operator <<
    inside BaseRecord, but then IMO the data encapsulation principal would
    be violated, because I'm able to call each member directly.
    Therefore I'd like to have a const member method named print to be
    called out of the operator to have access to the struct's internas.
    And by the way, there is mostly a better solution than having a friend
    declaration.

    You're right, I need of course an instance of tFielLenData to call my
    operator<< function.
    But the point is, that I don't want to create an object outside the
    class BaseRecord and therefore the compiler error is wrong, as I am
    not trying to instantiate an object at that point. It
    It's only a declaration, not an instantiation.

    Kind regards,
    Thomas



    Nicolas Pavlidis <> wrote in message news:<>...
    > (Thomas) writes:
    >
    > > Hi everybody!
    > > I have the following code:
    > >

    >
    >
    Code:
    >[color=green]
    > > From gcc-2.96 upwards I always get the following error message:
    > >
    > > In function `ostream &operator<< (ostream &, const
    > > BaseRecord::tFieldLenData &)':
    > > struct BaseRecord::tFieldLenData is protected
    > >
    > > I'm confused about this error, because even the type is declared as
    > > protected, I expect it to be usable as type within a typedef or
    > > function declaration outside the scope of it's outer class.
    > > The fact, that an object of type tFieldLenData cannot be instantiated
    > > outside the class BaseRecord is clear, but this should not care the
    > > compiler in this case.[/color]
    >
    > Why not? To be able to call this operator you need an instace of
    > BaseRecord::tFieldLenData, but this type is protected.
    >[color=green]
    > > My idea is, to have the possibilty to print the object to stdout or
    > > stderr from within the class itself, for instance for debugging
    > > purpose.[/color]
    >
    > Ok, try to make the two operator << function freinds of BaseRecord, that
    > only works is you only call this operator inside the class.
    >[color=green]
    > > I now have two questions concerning this point:
    > > 1.) Is this error message appropriate ?[/color]
    >
    > IMHO yes.
    >[color=green]
    > > 2.) If you say yes to no. 1, then what would be a nicer solution for
    > > this case in your opinion ?[/color]
    >
    > As said, try to make the two << operators friends of BaseRecord.
    >
    > Kind regards,
    > Nicolas[/color]
     
    Thomas, Sep 20, 2004
    #3
  4. Thomas

    DaKoadMunky Guest

    >I could solve it with a friend declaration of the operator <<
    >inside BaseRecord, but then IMO the data encapsulation principal would
    >be violated


    Here is what BS has to say about whether or not friends violate
    encapsulation...

    http://www.research.att.com/~bs/bs_faq2.html#friend

    Scott Meyers also has an article that discusses his thoughts regarding
    encapsulation...

    http://www.cuj.com/documents/s=8042/cuj0002meyers/

    Whether you use a member function or a non-member friend you will have exactly
    one function that is dependent upon the implementation details of your class.

    My thoughts are along the lines of Meyers and Stroustrup. Of course this is
    just MHO. :)

    I would be curious to know what you think after reading the links.
     
    DaKoadMunky, Sep 21, 2004
    #4
    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. Siemel Naran
    Replies:
    4
    Views:
    809
    Micah Cowan
    Jan 12, 2005
  2. Andy Lomax
    Replies:
    5
    Views:
    474
    John Carson
    Jun 30, 2005
  3. Replies:
    11
    Views:
    737
    James Kanze
    May 16, 2007
  4. Arkaitz Jimenez
    Replies:
    13
    Views:
    1,396
    Bart van Ingen Schenau
    May 12, 2009
  5. K. Frank
    Replies:
    8
    Views:
    653
    K. Frank
    Mar 22, 2012
Loading...

Share This Page