View Single Post
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: virtual class with friend functions

 
0
  #3
Dec 1st, 2008
What's a problem?
  1. class A {
  2. public:
  3. A() {}
  4. explicit A(const std::string& n):name(n) {}
  5. const std::string& getName() const {
  6. return name;
  7. }
  8. protected:
  9. std::string name;
  10. };
  11.  
  12. inline
  13. std::ostream& operator<<(std::ostream& os,const A& a) {
  14. os << a.getName();
  15. return os;
  16. }
  17.  
  18. class B: public A {
  19. public:
  20. B():A() {}
  21. explicit B(const std::string& n):A(n) {}
  22. };
  23.  
  24. inline
  25. std::ostream& operator<<(std::ostream& os,const B& b) {
  26. return operator<<(os,static_cast<const A&>(b));
  27. };
  28.  
  29. void abtest()
  30. {
  31. B dude("mbayabo");
  32. std::cout << dude << std::endl;
  33. }
Reply With Quote