| | |
virtual class with friend functions
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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?
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)
#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; }
comment out
and try
and give different names for base class and derived class 'name' variable to see the difference.
C++ Syntax (Toggle Plain Text)
virtual void mission();
and give different names for base class and derived class 'name' variable to see the difference.
thanks
-chandra
-chandra
What's a problem?
c++ Syntax (Toggle Plain Text)
class A { public: A() {} explicit A(const std::string& n):name(n) {} const std::string& getName() const { return name; } protected: std::string name; }; inline std::ostream& operator<<(std::ostream& os,const A& a) { os << a.getName(); return os; } class B: public A { public: B():A() {} explicit B(const std::string& n):A(n) {} }; inline std::ostream& operator<<(std::ostream& os,const B& b) { return operator<<(os,static_cast<const A&>(b)); }; void abtest() { B dude("mbayabo"); std::cout << dude << std::endl; }
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
mayabo's original code (other than the line
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
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.
![]() |
Similar Threads
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- Another C++ N00B Begging For Help (C++)
- need help in animal game guessing (C)
- Class Vectorization requirements (C++)
- C++ Identifiers and Keywords (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: Howmany byte is pointer?
- Next Thread: Help needed filling array with unique random numbers
| Thread Tools | Search this Thread |
ace_thread api array assignment based binary bitmap borland c++ c/c++ char class classes code coding compile connect console conversion count create() delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multidimensional multiple news node output pointer problem program programming project python random read reading recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






