Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Friend functions not inherited

Reply
Thread Tools

Friend functions not inherited

 
 
Andrea Crotti
Guest
Posts: n/a
 
      11-04-2010
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?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      11-04-2010
* 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>
 
Reply With Quote
 
 
 
 
Andrea Crotti
Guest
Posts: n/a
 
      11-04-2010
"Alf P. Steinbach /Usenet" <alf.p.steinbach+> 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?
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      11-04-2010
"Daniel T." <> 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...
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      11-04-2010
"Daniel T." <> 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:perator<< in that case?

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      11-04-2010
Andrea Crotti <> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-04-2010
On 11/4/2010 8:10 AM, Andrea Crotti wrote:
> "Daniel T."<> 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:perator<< 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:rint(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
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      11-05-2010
"Daniel T." <> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conflict between friend function and inherited class function ciccio C++ 4 01-18-2008 12:13 AM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Must See Deifferences between Friend & Best Friend wonderfulinfo@gmail.com Digital Photography 17 08-07-2006 01:34 PM
Friend brings a friend. Shisha Girl MCSE 4 03-03-2006 02:42 AM
Friend brings a friend. Shisha Girl Python 0 03-02-2006 02:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57