RSS Forums RSS

virtual class with friend functions

Please support our C++ advertiser: Programming Forums
Reply
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

  #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?

#include <iostream>
#include <string>
using namespace std;

class A {
public:
	string name;
	A(string sName) {
		name = sName;
	}
	virtual void mission();
	string getName() {
		return name;
	}
	friend ostream& operator << (ostream& out, A& oA) {
		out << oA.getName() << endl;
		return out;
	}
};

class B : public A {
public:
	B() : A("bob") {
		name = "bob";
	}
};

int main() {

	B DUDE;

	cout << DUDE;
        // this doesnt work either
        cout << (A)DUDE;

	return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 427
Reputation: Agni has a spectacular aura about Agni has a spectacular aura about Agni has a spectacular aura about 
Solved Threads: 65
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: virtual class with friend functions

  #2  
Dec 1st, 2008
comment out

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  
Posts: 2,000
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: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: virtual class with friend functions

  #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  
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 29
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: virtual class with friend functions

  #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 3:24 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 528 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:05 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC