Virtual function

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

    Virtual function

    I have declared some function virtual, but when I call this function I have
    errors.
    In a class Simulator I have defined this:
    Nave *navi[20];
    The virtual function is the function modifica().
    I call this function in this way: navi[i]->modifica();
    In this way, the function work correctly.
    But in another class Simulator I have defined:
    Veicolo *veicoli;
    The Virtual function of Veicolo is colled modifica() too.
    If I call this function in this way:
    veicoli[i]->modifica();
    I receive this error:
    error C2819: il tipo "Veicolo" has not a member"operator ->" of overload
    see the declaration of "Veicolo"
    Would you use"."?
    error C2227: the element on the left of "->modifica" must point to a class,
    structure o union
    the type is "Veicolo"
    Would you use"."?

    If I call the function in this way:
    veicoli[i].modifica();
    The compiler doesn't give me error, but I don't use the virtual function.
    The array of veicolo contain object of class tha derive from veicolo like
    Auto and Moto.
    When I call modifica in this way I use the function modifica of veicolo, but
    not the function modifica() of Auto or Moto.
    Whay this behaviour?
    In the example of Nave the virtual function modifica() works correctly.

    The last thing, I have defined this:
    char *name;
    and the method:
    void Person::setName (char *n){
    int i=static_cast<i nt>(strlen(n));
    noae = new char[i+1];
    strcpy(nome, n);
    }

    char* Person::getName ()const{
    return nome;
    }

    This function work correctly but if I define:
    char name[20];
    I cannot declare the function getName const, if I try to do thsi thing I
    receive this error:
    impossible to convert char[10] to char*, how can I resolve this error?
    Thanks.



  • Howard

    #2
    Re: Virtual function


    "Piotre Ugrumov" <aumulonenospam @tin.it> wrote in message
    news:QjgQb.1364 72$VW.5480743@n ews3.tin.it...[color=blue]
    > I have declared some function virtual, but when I call this function I[/color]
    have[color=blue]
    > errors.
    > In a class Simulator I have defined this:
    > Nave *navi[20];[/color]

    This defines an array of pointers. Each pointer should point to a separate
    instance of a Nave object.
    [color=blue]
    > The virtual function is the function modifica().
    > I call this function in this way: navi[i]->modifica();
    > In this way, the function work correctly.
    > But in another class Simulator I have defined:
    > Veicolo *veicoli;[/color]

    This points to just ONE object of type Veicolo. Above, you declared an
    ARRAY of pointers. Did you mean:

    Veicolo *veicoli[20];

    ?
    [color=blue]
    > The Virtual function of Veicolo is colled modifica() too.
    > If I call this function in this way:
    > veicoli[i]->modifica();[/color]

    You do not show how you allocated this "veicoli" array. If you intended to
    declare it as an array, then go back and modify the declaration so it is an
    array. If, instead, you dynamically allocated an array of Veicolo objects
    (using veicoli = new Veicolo[20]), then what you have is an array of Veicolo
    objects, not an array of pointers. In that case, yes, you would use the "."
    operator instead of the "->" operator. But I am guessing that what you
    really meant was to declare veicoli as an array of pointers instead of a
    single pointer, correct?

    -Howard


    Comment

    • Ron Natalie

      #3
      Re: Virtual function


      "Piotre Ugrumov" <aumulonenospam @tin.it> wrote in message news:QjgQb.1364 72$VW.5480743@n ews3.tin.it...[color=blue]
      > I have declared some function virtual, but when I call this function I have
      > errors.[/color]

      Your problem has nothing to do with virtual function.
      [color=blue]
      > I call this function in this way: navi[i]->modifica();[/color]
      navi[i] has tyhe Nave*. navi is an array of pointers.

      [color=blue]
      > Veicolo *veicoli;
      > The Virtual function of Veicolo is colled modifica() too.
      > If I call this function in this way:
      > veicoli[i]->modifica();[/color]

      veicoli[i] is of type Veicolo. It doesn't have a -> operator.
      It's not even clear whether veicoli points to more than one Veicolo
      object.
      Perhaps you mean
      veicoli->modifica();


      [color=blue]
      >
      > If I call the function in this way:
      > veicoli[i].modifica();[/color]

      If veicoli isn't a pointing to an array, indexing it by other than zero
      is going to be bogus. Where does the value of this pointer come from?
      [color=blue]
      > int i=static_cast<i nt>(strlen(n));[/color]

      What is the point of the above cast?
      [color=blue]
      > This function work correctly but if I define:
      > char name[20];
      > I cannot declare the function getName const, if I try to do thsi thing I
      > receive this error:
      > impossible to convert char[10] to char*, how can I resolve this error?
      >[/color]

      You're obviously screwing up something in the posting here. I suspect it
      really says it can't convert const char[20] to char*.

      In the const method, everything in the object itself is const. You have to return
      a const char*, not a char*. In the case where you use a pointer rather than an
      array, it's allocated elsewhere and not const.

      Frankly, you're better advised to just use the standard string class instead.

      Comment

      • Kevin Saff

        #4
        Re: Virtual function


        "Piotre Ugrumov" <aumulonenospam @tin.it> wrote in message
        news:QjgQb.1364 72$VW.5480743@n ews3.tin.it...[color=blue]
        > I have declared some function virtual, but when I call this function I[/color]
        have[color=blue]
        > errors.
        > In a class Simulator I have defined this:
        > Nave *navi[20];
        > The virtual function is the function modifica().
        > I call this function in this way: navi[i]->modifica();
        > In this way, the function work correctly.
        > But in another class Simulator I have defined:
        > Veicolo *veicoli;
        > The Virtual function of Veicolo is colled modifica() too.
        > If I call this function in this way:
        > veicoli[i]->modifica();[/color]

        Wouldn't you just say:
        veicoli-> modifica();

        since you only declared Veicolo *veicoli?

        You better show how you are initializing veicoli, because I fear you are
        doing something disastrously wrong (although syntactically correct).
        [color=blue]
        > I receive this error:
        > error C2819: il tipo "Veicolo" has not a member"operator ->" of overload
        > see the declaration of "Veicolo"
        > Would you use"."?
        > error C2227: the element on the left of "->modifica" must point to a[/color]
        class,[color=blue]
        > structure o union
        > the type is "Veicolo"
        > Would you use"."?
        >
        > If I call the function in this way:
        > veicoli[i].modifica();
        > The compiler doesn't give me error, but I don't use the virtual function.[/color]

        No, just because you don't use -> does not mean you do not use a virtual
        function. The array operator returns a reference (Veicolo&) which will
        still call the virtual function, if applicable. However, I fear you are
        doing something tremendously wrong here. I am guessing you initialize as:

        veicoli = new Veicolo [n];

        And then later you do something like:

        veicoli[5] = VeicoloSubclass (blahblah);

        I promise, this does not do what you expect it to. You do not actually get
        a VeicoloSubclass in the array, instead, the VeicoloSubclass object is
        copied into a Veicolo object in the array.

        Basically you should not use arrays when you also need polymorphism.
        Instead std::vector often helps:

        std::vector <Veicolo*> veicoli;
        (see vector docs for more info)

        [color=blue]
        > The array of veicolo contain object of class tha derive from veicolo like
        > Auto and Moto.
        > When I call modifica in this way I use the function modifica of veicolo,[/color]
        but[color=blue]
        > not the function modifica() of Auto or Moto.
        > Whay this behaviour?
        > In the example of Nave the virtual function modifica() works correctly.
        >
        > The last thing, I have defined this:
        > char *name;
        > and the method:
        > void Person::setName (char *n){
        > int i=static_cast<i nt>(strlen(n));
        > noae = new char[i+1];
        > strcpy(nome, n);
        > }
        >
        > char* Person::getName ()const{
        > return nome;
        > }
        >
        > This function work correctly but if I define:
        > char name[20];
        > I cannot declare the function getName const, if I try to do thsi thing I
        > receive this error:
        > impossible to convert char[10] to char*, how can I resolve this error?
        > Thanks.[/color]

        Don't use raw character pointers. Instead use std::string.

        HTH
        --
        KCS


        Comment

        Working...