virtual class with friend functions

Reply

Join Date: Oct 2008
Posts: 2
Reputation: mbayabo is an unknown quantity at this point 
Solved Threads: 0
mbayabo mbayabo is offline Offline
Newbie Poster

virtual class with friend functions

 
0
  #1
Dec 1st, 2008
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?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class A {
  6. public:
  7. string name;
  8. A(string sName) {
  9. name = sName;
  10. }
  11. virtual void mission();
  12. string getName() {
  13. return name;
  14. }
  15. friend ostream& operator << (ostream& out, A& oA) {
  16. out << oA.getName() << endl;
  17. return out;
  18. }
  19. };
  20.  
  21. class B : public A {
  22. public:
  23. B() : A("bob") {
  24. name = "bob";
  25. }
  26. };
  27.  
  28. int main() {
  29.  
  30. B DUDE;
  31.  
  32. cout << DUDE;
  33. // this doesnt work either
  34. cout << (A)DUDE;
  35.  
  36. return 0;
  37. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 439
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: virtual class with friend functions

 
0
  #2
Dec 1st, 2008
comment out

  1. virtual void mission();
and try

and give different names for base class and derived class 'name' variable to see the difference.
thanks
-chandra
Reply With Quote Quick reply to this message  
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

 
1
  #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 Quick reply to this message  
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC