Virtual function

P

Piotre Ugrumov

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->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->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.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<int>(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.
 
H

Howard

Piotre Ugrumov said:
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];

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


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

Veicolo *veicoli[20];

?
The Virtual function of Veicolo is colled modifica() too.
If I call this function in this way:
veicoli->modifica();


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
 
R

Ron Natalie

Piotre Ugrumov said:
I have declared some function virtual, but when I call this function I have
errors.

Your problem has nothing to do with virtual function.
I call this function in this way: navi->modifica();

navi has tyhe Nave*. navi is an array of pointers.

Veicolo *veicoli;
The Virtual function of Veicolo is colled modifica() too.
If I call this function in this way:
veicoli->modifica();


veicoli 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();


If I call the function in this way:
veicoli.modifica();


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?
int i=static_cast<int>(strlen(n));

What is the point of the above cast?
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?

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.
 
K

Kevin Saff

Piotre Ugrumov said:
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->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->modifica();


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).
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.modifica();
The compiler doesn't give me error, but I don't use the virtual function.


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)

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<int>(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.

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

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top