Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Howto declare friend operator virtual??? [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Tue Feb 18, 2003 3:11 pm    Post subject: Howto declare friend operator virtual??? [SOLVED] Reply with quote

Hi folks,

as I was unable to find any resources on the internet, i'm still wondering howto declare a friend operator virtual???
for example: t.h
Code:

#include <iostream>

using namesapce std;
class T {
public:
T();
~T();
virtual int overrideme();
fiend ostream& operator << (ostream&,T&);
}

The problem is howto make "operator <<" virtual, as i'm only able to define that in subclasses from T!!!
Any help is welcome!

Thanx, bye, Fox


Last edited by Starfox on Wed Feb 19, 2003 9:34 pm; edited 1 time in total
Back to top
View user's profile Send private message
brad
n00b
n00b


Joined: 29 Nov 2002
Posts: 6

PostPosted: Tue Feb 18, 2003 4:27 pm    Post subject: Reply with quote

Here's my suggestion:

class T {
T();
~T();

virtual ostream& printme(ostream&);
};

ostream& operator<<(ostream& out, T& t) {
return t.printme(out);
};

I don't think you can have virtual friends, but someone out there might prove me wrong. I'm not completely up to date on the most recent changes to the standard. However, I think this will fill your needs and you don't even need a friend (assuming printme() is public)..
Back to top
View user's profile Send private message
TwoSlick
Tux's lil' helper
Tux's lil' helper


Joined: 22 Apr 2002
Posts: 114
Location: Rolla, MO

PostPosted: Wed Feb 19, 2003 5:48 am    Post subject: Reply with quote

Quote:
The problem is howto make "operator <<" virtual, as i'm only able to define that in subclasses from T!!!


I believe the whole purpose of having virtual functions is to allow generic member function calls through an inheritance hierarchy. Virtual functions only have a scope within their own class/subclasses.

I'm not sure why you would want to have a friend function declared as a virtual function. A friend function is not part of the class declaring it as a friend, and therefore cannot act as a virtual replacement. Any particular purpose you are trying this?

Maybe we can help you solve your problem without trying virtual functions.

- TwoSlick
Back to top
View user's profile Send private message
Starfox
Tux's lil' helper
Tux's lil' helper


Joined: 04 Sep 2002
Posts: 93

PostPosted: Wed Feb 19, 2003 9:33 pm    Post subject: Reply with quote

@brad

your idea works!! Thank you very much!

@2Slick:

The problem was i had a base class T and very many subclasses x1..xn from T. And i had a class doSomething that does something for every x1..xn. But it does always the "same" thing! So I needed a common interface, and exactly that is served by class T. So i write my doSomething() function once, and it works for every x1..xn!

bye fox
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum