Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Friend functions not inherited (http://www.velocityreviews.com/forums/t737192-friend-functions-not-inherited.html)

Andrea Crotti 11-04-2010 10:37 AM

Friend functions not inherited
 
To my surprise I read that some time ago, so what I thought it could
work in fact it could not work.

Supposing I have two classes

class Base
friend operator<<(..)
// print base fields

class Extended : Base

Since the friend is not inherited I guess I have to declare it again,
but supposing I want to
- first call the << on the base class
- then call on the extended class

how should I do that?
A cast from the object itself ot it's base type?

Alf P. Steinbach /Usenet 11-04-2010 10:53 AM

Re: Friend functions not inherited
 
* Andrea Crotti, on 04.11.2010 11:37:
> To my surprise I read that some time ago, so what I thought it could
> work in fact it could not work.
>
> Supposing I have two classes
>
> class Base
> friend operator<<(..)
> // print base fields
>
> class Extended : Base
>
> Since the friend is not inherited I guess I have to declare it again,
> but supposing I want to
> - first call the<< on the base class
> - then call on the extended class
>
> how should I do that?
> A cast from the object itself ot it's base type?


Please post a minimal and complete example that exemplifies the problem.

See the FAQ item about how to post a question about Code That Does Not Work.


Cheers & hth.

- Alf

--
blog at <url: http://alfps.wordpress.com>

Andrea Crotti 11-04-2010 11:09 AM

Re: Friend functions not inherited
 
"Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> writes:

>
> Please post a minimal and complete example that exemplifies the problem.
>
> See the FAQ item about how to post a question about Code That Does Not Work.
>
>
> Cheers & hth.
>
> - Alf


Nothing in particular that doesn't work, but this is what I wanted to do

--8<---------------cut here---------------start------------->8---
#include <iostream>

using namespace std;

class Base
{
friend ostream& operator<<(ostream& s, const Base& c);
};

class Extended : public Base
{
};

ostream& operator<<(ostream& s, const Base& b)
{
s << "base class" << endl;
return s;
}

ostream& operator<<(ostream& s, const Extended& e)
{
s << (*((Base *) &e)) << endl;
}

int main() {
Extended e;
cout << e;
return 0;
}
--8<---------------cut here---------------end--------------->8---

Which is rather ugly, is there a more automatic way to call something on
the base class in friend operators?

Andrea Crotti 11-04-2010 12:08 PM

Re: Friend functions not inherited
 
"Daniel T." <daniel_t@earthlink.net> writes:

> What's wrong with doing this?
>
> class Base {
> friend ostream& operator<<(ostream& s, const Base& c);
> };
>
> class Extended : public Base { };
>
> ostream& operator<<(ostream& s, const Base& b) {
> s << "base class" << endl;
> return s;
> }
>
> int main() {
> Extended e;
> cout << e << '\n';
> }
>
> The above compiles fine and does what you wanted.


Right it does :)
I guess than that what I found on the internet was just wrong...

Andrea Crotti 11-04-2010 12:10 PM

Re: Friend functions not inherited
 
"Daniel T." <daniel_t@earthlink.net> writes:

>
> What's wrong with doing this?
>
> class Base {
> friend ostream& operator<<(ostream& s, const Base& c);
> };
>
> class Extended : public Base { };
>
> ostream& operator<<(ostream& s, const Base& b) {
> s << "base class" << endl;
> return s;
> }
>
> int main() {
> Extended e;
> cout << e << '\n';
> }
>
> The above compiles fine and does what you wanted.



and what if you want instead want to print
"extended class base class"

So first calling the specialized method and from it the base class?

How do I tell to use Base::operator<< in that case?


Juha Nieminen 11-04-2010 01:02 PM

Re: Friend functions not inherited
 
Andrea Crotti <andrea.crotti.0@gmail.com> wrote:
> I guess than that what I found on the internet was just wrong...


No, it was not wrong. You simply misunderstood it.

The point is that the friend function in your example doesn't *need*
to be inherited to the derived class in order for it to work properly.

Victor Bazarov 11-04-2010 01:20 PM

Re: Friend functions not inherited
 
On 11/4/2010 8:10 AM, Andrea Crotti wrote:
> "Daniel T."<daniel_t@earthlink.net> writes:
>
>>
>> What's wrong with doing this?
>>
>> class Base {
>> friend ostream& operator<<(ostream& s, const Base& c);
>> };
>>
>> class Extended : public Base { };
>>
>> ostream& operator<<(ostream& s, const Base& b) {
>> s<< "base class"<< endl;
>> return s;
>> }
>>
>> int main() {
>> Extended e;
>> cout<< e<< '\n';
>> }
>>
>> The above compiles fine and does what you wanted.

>
>
> and what if you want instead want to print
> "extended class base class"
>
> So first calling the specialized method and from it the base class?
>
> How do I tell to use Base::operator<< in that case?


You need to introduce polymorphism and call a virtual function in the
class Base. Something like

class Base {
...
virtual ostream& print(ostream& os) const
{
return os << "base class";
}
};

class Extended : public Base {
ostream& print(ostream& os) const
{
os << "extended class ";
return Base::print(os);
}
};

ostream& operator << (ostream& os, const Base& c)
{
return c.print(os);
}

V
--
I do not respond to top-posted replies, please don't ask

Andrea Crotti 11-05-2010 09:44 AM

Re: Friend functions not inherited
 
"Daniel T." <daniel_t@earthlink.net> writes:

> You could do as Victor Bazarov suggested, but the following might be
> simpler:
>
> class Base { };
>
> class Extended : public Base { };
>
> ostream& operator<<(ostream& s, const Base& b) {
> s << "base class" << endl;
> return s;
> }
>
> ostream& operator<<(ostream& s, const Extended& b) {
> s << static_cast<const Base&>(b);
> s << "extended class" << endl;
> return s;
> }
>
> int main() {
> Extended e;
> cout << e << '\n';
> }


Ah yes I like this solution also thanks!


All times are GMT. The time now is 08:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.