![]() |
| ||
| virtual class with friend functions this is a similar example to what i have. it's simplified but it still get's the same error. i need to be able to use << operator to print out all information from class A. i have no idea how to do that. i tried casting it as an A object but then it starts telling me that i can do that with virtual functions. is there any way to go around this? #include <iostream> |
| ||
| Re: virtual class with friend functions comment out virtual void mission();and try and give different names for base class and derived class 'name' variable to see the difference. |
| ||
| Re: virtual class with friend functions What's a problem? class A { |
| ||
| Re: virtual class with friend functions mayabo's original code (other than the line cout << (A)DUDE;) should actually work. The line name="bob";in B's constructor is not required (and A::name can be made private). The compiler is able to do the conversion "derived class to public base" (i.e. B to A) implicitly. If you're getting errors, then either there is some code in play not being shown or you have a buggy compiler (really old versions of VC++ come to mind). It would probably be better to make getName() a const method (optionally virtual) and for the operator<<() to have its second argument a const reference. And "using namespace std;"before a class definition: wash your mouth out with soap, particularly if you do that in a header file..... |
| All times are GMT -4. The time now is 9:19 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC