Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. about virtual function's return type

about virtual function's return type

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bobi_zcl
    wrote on last edited by
    #1

    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

    T A T S 4 Replies Last reply
    0
    • B bobi_zcl

      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

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • B bobi_zcl

        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

        A Offline
        A Offline
        Aamir Butt
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B bobi_zcl

          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

          T Offline
          T Offline
          Taka Muraoka
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B bobi_zcl

            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

            S Offline
            S Offline
            sunit5
            wrote on last edited by
            #5

            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

            B 1 Reply Last reply
            0
            • S sunit5

              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

              B Offline
              B Offline
              bobi_zcl
              wrote on last edited by
              #6

              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

              S 1 Reply Last reply
              0
              • B bobi_zcl

                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

                S Offline
                S Offline
                sunit5
                wrote on last edited by
                #7

                pls tell me the error Also have define function func1 for base and for derived as well. never say die

                B 1 Reply Last reply
                0
                • S sunit5

                  pls tell me the error Also have define function func1 for base and for derived as well. never say die

                  B Offline
                  B Offline
                  bobi_zcl
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups