Inheritance Issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew

    Inheritance Issues

    Hi, I have a base class called Item with a virtual function act. It's
    subclass Sword defines the function again doing something different
    from the first. It store the sword in a vector of items and call the
    act function and, as to be expected, it calls the one from Item. How
    can I get it to call the one from sword?
  • Russ Ford

    #2
    Re: Inheritance Issues

    On 20 Nov 2003 12:50:00 -0800, mattheww@nessin ess.com (Matthew) wrote:

    Did you declare act as virtual in the Sword class?
    [color=blue]
    >Hi, I have a base class called Item with a virtual function act. It's
    >subclass Sword defines the function again doing something different
    >from the first. It store the sword in a vector of items and call the
    >act function and, as to be expected, it calls the one from Item. How
    >can I get it to call the one from sword?[/color]

    Comment

    • Mike Wahler

      #3
      Re: Inheritance Issues


      "Russ Ford" <russford@shaw. ca> wrote in message
      news:igbqrvcmg2 2k6kmoijt5lpdfv mh4c00lbq@4ax.c om...[color=blue]
      > On 20 Nov 2003 12:50:00 -0800, mattheww@nessin ess.com (Matthew) wrote:
      >
      > Did you declare act as virtual in the Sword class?[/color]

      Not necessary (as long as the signature is the same).

      The real issue is: *How* was the function called?
      Polymorphism only works via a pointer or reference.

      Matthew:

      We can only make guesses like this since you show no code.

      Show the code.

      -Mike


      Comment

      • Mike Wahler

        #4
        Re: Inheritance Issues


        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
        news:qpavb.8552 $n56.1935@newsr ead1.news.pas.e arthlink.net...[color=blue]
        >
        > "Russ Ford" <russford@shaw. ca> wrote in message
        > news:igbqrvcmg2 2k6kmoijt5lpdfv mh4c00lbq@4ax.c om...[color=green]
        > > On 20 Nov 2003 12:50:00 -0800, mattheww@nessin ess.com (Matthew) wrote:
        > >
        > > Did you declare act as virtual in the Sword class?[/color]
        >
        > Not necessary (as long as the signature is the same).[/color]

        Clarification: if the signature is not the same, then
        it's not an override anyway.

        -Mike


        Comment

        • lilburne

          #5
          Re: Inheritance Issues

          Matthew wrote:
          [color=blue]
          > Hi, I have a base class called Item with a virtual function act. It's
          > subclass Sword defines the function again doing something different
          > from the first. It store the sword in a vector of items and call the
          > act function and, as to be expected, it calls the one from Item. How
          > can I get it to call the one from sword?[/color]

          If you have vector<Item> then you are stuffed because what
          is stored in the vector is Item's the Sword part is stripped
          off, lookup class slicing (no pun intended) in your book.
          What you want is either a vector of pointer to Items
          vector<Item*>, or more preferably a vector<
          smart_pointer<I tem> >. Where 'smart_pointer' is your
          favourite wrapper for pointers (boost::shared_ ptr at a pinch
          I suppose).

          Comment

          • jeffc

            #6
            Re: Inheritance Issues


            "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
            news:vqavb.8555 $n56.6612@newsr ead1.news.pas.e arthlink.net...[color=blue]
            >
            > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
            > news:qpavb.8552 $n56.1935@newsr ead1.news.pas.e arthlink.net...[color=green]
            > >
            > > "Russ Ford" <russford@shaw. ca> wrote in message
            > > news:igbqrvcmg2 2k6kmoijt5lpdfv mh4c00lbq@4ax.c om...[color=darkred]
            > > > On 20 Nov 2003 12:50:00 -0800, mattheww@nessin ess.com (Matthew) wrote:
            > > >
            > > > Did you declare act as virtual in the Sword class?[/color]
            > >
            > > Not necessary (as long as the signature is the same).[/color]
            >
            > Clarification: if the signature is not the same, then
            > it's not an override anyway.[/color]

            If it's not virtual, it's never an override regardless of the signature.


            Comment

            Working...