View Single Post
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: virtual class with friend functions

 
0
  #4
Dec 1st, 2008
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.....
Last edited by grumpier; Dec 1st, 2008 at 4:24 pm.
Reply With Quote