View previous topic :: View next topic |
Author |
Message |
Starfox Tux's lil' helper
Joined: 04 Sep 2002 Posts: 93
|
Posted: Tue Feb 18, 2003 3:11 pm Post subject: Howto declare friend operator virtual??? [SOLVED] |
|
|
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 |
|
|
brad n00b
Joined: 29 Nov 2002 Posts: 6
|
Posted: Tue Feb 18, 2003 4:27 pm Post subject: |
|
|
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 |
|
|
TwoSlick Tux's lil' helper
Joined: 22 Apr 2002 Posts: 114 Location: Rolla, MO
|
Posted: Wed Feb 19, 2003 5:48 am Post subject: |
|
|
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 |
|
|
Starfox Tux's lil' helper
Joined: 04 Sep 2002 Posts: 93
|
Posted: Wed Feb 19, 2003 9:33 pm Post subject: |
|
|
@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 |
|
|
|