943,521 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 916
  • C++ RSS
Dec 1st, 2008
0

virtual class with friend functions

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mbayabo is offline Offline
2 posts
since Oct 2008
Dec 1st, 2008
0

Re: virtual class with friend functions

comment out

C++ Syntax (Toggle Plain Text)
  1. virtual void mission();
and try

and give different names for base class and derived class 'name' variable to see the difference.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 1st, 2008
1

Re: virtual class with friend functions

What's a problem?
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 1st, 2008
0

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.....
Last edited by grumpier; Dec 1st, 2008 at 4:24 pm.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Howmany byte is pointer?
Next Thread in C++ Forum Timeline: Help needed filling array with unique random numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC