zhwcn Posted September 11, 2004 Share Posted September 11, 2004 #include<iostream.h> class Computer { public: ? ? ? virtual ?void ?name() const; }; class Pc: public Computer { public: ? ? ? virtual void name() const; }; void Computer::name() const { ? ? ? ? cout<<"Computer"<<endl; } void Pc::name() const { ? ? ? ? cout<<"Pc"<<endl; } void print(Computer); main() { ? ? ? ? Computer c=Pc(); ? ? ? ? print(c); ? ? ? ? return 0; } void print(Computer &cc) ? ? /* if take out the "&" symbol , the program can run correctly. I want to know WHY???*/ { ? ? ? ? cc.name(); } Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 11, 2004 Share Posted September 11, 2004 void print(Computer &cc) This function is requesting a reference to a Computer object. So if you want to pass an argument to this function, you need to pass a reference (in your calling code you are attempting to pass a copy of an object). So you can either do this: void print (Computer &cc) { cc->name(); } //Call the function like this: print (&c); Or you can do this: void print(Computer cc) { cc.name(); } //Call the function like this: print (c); But you can't mix and match. Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 11, 2004 Author Share Posted September 11, 2004 void print(Computer &cc) This function is requesting a reference to a Computer object. So if you want to pass an argument to this function, you need to pass a reference (in your calling code you are attempting to pass a copy of an object). So you can either do this: void print (Computer &cc) { ? ?cc->name(); } //Call the function like this: print (&c); Or you can do this: void print(Computer cc) { ? ?cc.name(); } //Call the function like this: print (c); But you can't mix and match. void print (Computer &cc) { cc->name(); } print(&c); ************** I don't think you are right. This should be right void print (Computer *cc) { cc->name(); } and call function like : print(&c) Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 11, 2004 Share Posted September 11, 2004 Nope, I'm not talking about a pointer. void print(Computer &cc) {...} //This is expecting a reference to a Computer void print(Computer *cc) {...} //This is expecting a pointer to a Computer print(&cc); //IIRC, Both are called like this Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 12, 2004 Author Share Posted September 12, 2004 Nope, I'm not talking about a pointer. void print(Computer &cc) {...} //This is expecting a reference to a Computer void print(Computer *cc) {...} //This is expecting a pointer to a Computer print(&cc); //IIRC, Both are called like this Your code can't work, you can try . Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 12, 2004 Author Share Posted September 12, 2004 (edited) I repaired my codes again, now it can work correctly. #include<iostream.h> class Computer { public: ? ? ?virtual ?void ?name() const; }; class Pc: public Computer { public: ? ? ?virtual void name() const; }; void Computer::name() const { ? ? ? ?cout<<"Computer"<<endl; } void Pc::name() const { ? ? ? ?cout<<"Pc"<<endl; } void print(Computer &); main() { ? ? ? ?Pc c; ? ? ? ?print(c); ? ? ? ?return 0; } void print(Computer &cc) ? ? { ? ? ? ?cc.name(); } HI dannysmurf, my codes can prove:laugh:e :pbb:pem:pic:p' alt=':)' />ong... :p :p :p :p :laugh: :) Edited September 12, 2004 by zhwcn Link to comment Share on other sites More sharing options...
0 smurfiness Posted September 12, 2004 Share Posted September 12, 2004 Possibly. It's been a long time since I've used a reference. But why are you using a reference? Link to comment Share on other sites More sharing options...
0 zhwcn Posted September 12, 2004 Author Share Posted September 12, 2004 Possibly. It's been a long time since I've used a reference. But why are you using a reference? Use reference , the program print "Pc",else you can only print "Computer". Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted September 12, 2004 Share Posted September 12, 2004 On your first code: Computer c=Pc(); That should be = new Pc(); On the subject of references vs pointers: References just translate into a constant pointer anyways during compilation. Link to comment Share on other sites More sharing options...
Question
zhwcn
#include<iostream.h> class Computer { public: ? ? ? virtual ?void ?name() const; }; class Pc: public Computer { public: ? ? ? virtual void name() const; }; void Computer::name() const { ? ? ? ? cout<<"Computer"<<endl; } void Pc::name() const { ? ? ? ? cout<<"Pc"<<endl; } void print(Computer); main() { ? ? ? ? Computer c=Pc(); ? ? ? ? print(c); ? ? ? ? return 0; } void print(Computer &cc) ? ? /* if take out the "&" symbol , the program can run correctly. I want to know WHY???*/ { ? ? ? ? cc.name(); }Link to comment
Share on other sites
8 answers to this question
Recommended Posts