J
Jörg Rolef
Hello,
I'd like to do a virtual overload of the operator<. If I do it like in
the example listed below, then I get an error message for accessing op.b
in SpecialContents:perator<(): "Member element Contents:p.b is
protected."
But I do not get an error for access of op.a in Contents:perator<().
First: Do I have to use public member functions to access the values of
the second operand? (First I thought that declaring the members as
protected would do me the job, but of course this seems only to make the
object's own elements acessible to descendant classes.)
Second: Why does the access of op.a in Contents:perator<() not lead to
an error message then? At least, op is a foreign object, so this access
should also lead to an error - am I wrong?
Greetings and thank you,
Jörg
class Contents
{
protected:
int a;
char b;
public:
// ...
virtual bool operator<(Contents& op);
};
bool Contents:perator<(Contents& op)
{
return (a < op.a);
// Returns no error for the access of op.a
}
class SpecialContents : public Contents
{
bool operator<(Contents& op);
};
bool SpecialContents:perator<(Contents& op)
{
return (b < op.b);
// Returns error: "Contents:p.b is protected"
}
I'd like to do a virtual overload of the operator<. If I do it like in
the example listed below, then I get an error message for accessing op.b
in SpecialContents:perator<(): "Member element Contents:p.b is
protected."
But I do not get an error for access of op.a in Contents:perator<().
First: Do I have to use public member functions to access the values of
the second operand? (First I thought that declaring the members as
protected would do me the job, but of course this seems only to make the
object's own elements acessible to descendant classes.)
Second: Why does the access of op.a in Contents:perator<() not lead to
an error message then? At least, op is a foreign object, so this access
should also lead to an error - am I wrong?
Greetings and thank you,
Jörg
class Contents
{
protected:
int a;
char b;
public:
// ...
virtual bool operator<(Contents& op);
};
bool Contents:perator<(Contents& op)
{
return (a < op.a);
// Returns no error for the access of op.a
}
class SpecialContents : public Contents
{
bool operator<(Contents& op);
};
bool SpecialContents:perator<(Contents& op)
{
return (b < op.b);
// Returns error: "Contents:p.b is protected"
}