protected member class and operator overloading

T

Thomas

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
 
N

Nicolas Pavlidis

Hi everybody!
I have the following code:

Code:
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.[/QUOTE]

Why not? To be able to call this operator you need an instace of
BaseRecord::tFieldLenData, but this type is protected.
[QUOTE]
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.[/QUOTE]

Ok, try to make the two operator << function freinds of BaseRecord, that
only works is you only call this operator inside the class.
[QUOTE]
I now have two questions concerning this point:
1.) Is this error message appropriate ?[/QUOTE]

IMHO yes.
[QUOTE]
2.) If you say yes to no. 1, then what would be a nicer solution for
this case in your opinion ?[/QUOTE]

As said, try to make the two << operators friends of BaseRecord.

Kind regards,
Nicolas
 
T

Thomas

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 said:
Hi everybody!
I have the following code:

Code:
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.[/QUOTE]

Why not? To be able to call this operator you need an instace of
BaseRecord::tFieldLenData, but this type is protected.
[QUOTE]
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.[/QUOTE]

Ok, try to make the two operator << function freinds of BaseRecord, that
only works is you only call this operator inside the class.
[QUOTE]
I now have two questions concerning this point:
1.) Is this error message appropriate ?[/QUOTE]

IMHO yes.
[QUOTE]
2.) If you say yes to no. 1, then what would be a nicer solution for
this case in your opinion ?[/QUOTE]

As said, try to make the two << operators friends of BaseRecord.

Kind regards,
Nicolas[/QUOTE]
 
D

DaKoadMunky

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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top