access of protected function members from friends



 DEVELOP > c-Plus-Plus > access of protected function members from friends

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "fabio de francesco"
Date: 22 Jun 2004 08:46:44 AM
Object: access of protected function members from friends
Hello,
I have written this code that compiles without errors ( "..." stays
for code that I don't post for the sake of brevity):
// Person.h
....
class Person
{
public:
Person();
...
protected:
...
ostream& writeToConsole( ostream & ) {...};
istream& readFromConsole( istream & ) {...};
friend ostream& operator<<( ostream &out, Person &prs )
{ return prs.writeToConsole(out); }
friend istream& operator>>( istream &in, Person &prs )
{ return prs.readFromConsole(in); }
};
....
If I try to extract the code from the friend functions and put it in a
different compilation unit, like this:
// Person.h
....
class Person
{
public:
Person();
...
protected:
...
ostream& writeToConsole( ostream & ) {...};
istream& readFromConsole( istream & ) {...};
friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );
};
....
// Person.cpp
....
inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}
inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}
....
I got the following from the compiler:
/sources/C++_studio/datastructures/database/src/person.h: In function
`
*std::ostream& operator<<(std::ostream&, Person)':
*/sources/C++_studio/datastructures/database/src/person.h:39: error: `
std::ostream& Person::writeToConsole(std::ostream&)' is protected
*/sources/C++_studio/datastructures/database/src/person.cpp:24: error:
within this context
*/sources/C++_studio/datastructures/database/src/person.h: In function
`
*std::istream& operator>>(std::istream&, Person)':
*/sources/C++_studio/datastructures/database/src/person.h:40: error: `
std::istream& Person::readFromConsole(std::istream&)' is protected
*/sources/C++_studio/datastructures/database/src/person.cpp:29: error:
within this context
*gmake[2]: *** [person.o] Error 1
Please would someone explain what I am missing? In particular I would
like to know why these errors come out only if I put the friends
implementation on a different compilation unit.
Thank you in advance for every help.
Ciao,
Fabio De Francesco
.

User: "Christian Janßen"

Title: Re: access of protected function members from friends 22 Jun 2004 10:24:36 AM
[snip]

friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );

Refs here..

inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}

inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}

Objs here...
=> not friends.
.
User: "Howard"

Title: Re: access of protected function members from friends 22 Jun 2004 02:23:03 PM
"Christian Janßen" <cj@christian-janszen.de> wrote in message
news:40d84f37_1@news.arcor-ip.de...

[snip]

friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );

Refs here..

inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}

inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}


Objs here...
=> not friends.


In case that wasn't clear, what he means is that your Person parameters are
defined as reference parameters in the friend declaratons, but as by-value
parameters in the function definitions. Therefore, they are not the friends
described in the class, but actually completely different functions.
-Howard
.
User: "fabio de francesco"

Title: Re: access of protected function members from friends 23 Jun 2004 12:25:23 PM
"Howard" <alicebt@hotmail.com> wrote in message news:<rG%Bc.116282$Gx4.29709@bgtnsc04-news.ops.worldnet.att.net>...

In case that wasn't clear, what he means is that your Person parameters are
defined as reference parameters in the friend declaratons, but as by-value
parameters in the function definitions. Therefore, they are not the friends
described in the class, but actually completely different functions.

I'm sorry, what a silly oversight!
I didn't check the correspondance between parameters becouse I was
sure to have copied the functions as they were in the class definition
and furthermore I was misguided from the resulting errors.
I always rely on the compiler that says it cannot find the candidate
in class definition when I don't use the parameters as they were
declared in the class; but this time they were friend functions and
so ...
One more reason to limit the use of "friends", I suppose.
Thank you,
Fabio De Francesco
.




  Page 1 of 1

1

 


Related Articles
access private member function
How to access a class member function outside of its class?
How come I am able to access my private function
how to access a private member function without using friend and virtual
Can't access class members from function
Friend in Class Can't Access Private Function
Templated friend function can't access private members?
Changing access specifier for virtual function
how to declare a friend function that can access two class
Can't access a static private data member from a friend function?
Binary predicate member function needs access to another object's data
Problem with pushback function - "An Access Violation.. "
access to private function.
why C++ object can access its data member without via getter function??
access private member function through a function having template type
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER