about virtual function's return type
-
Hi:this is my codes: class CIPerson { int m_nID; }; class CStudent:public CIPerson { int m_nOld; }; class CIPersonInfo { virtual CIPerson & search()=0; }; class CStudentInfo:public CIPersonInfo { CStudent & search(){} }; can you tell me why "CStudent & search(){}" is error?CStudent is derived from CIPerson your friend:bobi
-
Hi:this is my codes: class CIPerson { int m_nID; }; class CStudent:public CIPerson { int m_nOld; }; class CIPersonInfo { virtual CIPerson & search()=0; }; class CStudentInfo:public CIPersonInfo { CStudent & search(){} }; can you tell me why "CStudent & search(){}" is error?CStudent is derived from CIPerson your friend:bobi
firstly, why do you create two new classes xxxInfo for the search methods ? secondly, what is the error returned ? thirdly, did you notice that your base classes data members were private (where they should be protected) ?
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 5:47 Tuesday 24th January, 2006 -
Hi:this is my codes: class CIPerson { int m_nID; }; class CStudent:public CIPerson { int m_nOld; }; class CIPersonInfo { virtual CIPerson & search()=0; }; class CStudentInfo:public CIPersonInfo { CStudent & search(){} }; can you tell me why "CStudent & search(){}" is error?CStudent is derived from CIPerson your friend:bobi
You have an empty body of Search() method. You should return a CStudent object. toxcct has pointed other things which are equally valid as well.
-
Hi:this is my codes: class CIPerson { int m_nID; }; class CStudent:public CIPerson { int m_nOld; }; class CIPersonInfo { virtual CIPerson & search()=0; }; class CStudentInfo:public CIPersonInfo { CStudent & search(){} }; can you tell me why "CStudent & search(){}" is error?CStudent is derived from CIPerson your friend:bobi
You can't override functions only by return type. Both search() methods need to return exactly the same type. It doesn't matter that one is derived from the other, they must be *exactly* the same.
The two most common elements in the universe are Hydrogen and stupidity. - Harlan Ellison Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
-
Hi:this is my codes: class CIPerson { int m_nID; }; class CStudent:public CIPerson { int m_nOld; }; class CIPersonInfo { virtual CIPerson & search()=0; }; class CStudentInfo:public CIPersonInfo { CStudent & search(){} }; can you tell me why "CStudent & search(){}" is error?CStudent is derived from CIPerson your friend:bobi
signature of the virtual functions in base and derived class must be same .but their returns types can be different only if both returns have base and derived relation ship class CBase { virtual CBase* func1(int,char); }; class CDerived { CDerived *func19int,char); }; never say die
-
signature of the virtual functions in base and derived class must be same .but their returns types can be different only if both returns have base and derived relation ship class CBase { virtual CBase* func1(int,char); }; class CDerived { CDerived *func19int,char); }; never say die
yes,I think you have understand, I want to know it is right that virtual functions's returns types can be different only if both returns have base and derived relation ship,but follow codes is error in Visual C++ 6.0: class CBase { virtual CBase* func1(int,char); }; class CDerived:public CBase { CDerived *func1(int,char); }; can you tell me why? your friend:bobi
-
yes,I think you have understand, I want to know it is right that virtual functions's returns types can be different only if both returns have base and derived relation ship,but follow codes is error in Visual C++ 6.0: class CBase { virtual CBase* func1(int,char); }; class CDerived:public CBase { CDerived *func1(int,char); }; can you tell me why? your friend:bobi
-
pls tell me the error Also have define function func1 for base and for derived as well. never say die
class CBase { virtual CBase* func1(int,char); }; class CDerived:public CBase { CDerived *func1(int,char); }; I complier codes in VC 6.0 and diplay follow error: error C2555: 'CDerived::func1' : overriding virtual function differs from 'CBase::func1' only by return type or calling convention because CDerived derived from CBase, I think CDerived::func1 can return CDerived's pointer.do you think so ? your friend:bobi