samedi 19 avril 2014

OOP - c ++ nouvelle implémentation de fonction virtuelle - Stack Overflow


even i think that question is stupid. but i've a little experience.


i have a base class that has such method:


class A{  virtual void func(int)=0 };

and inherited class


    class  B :public A
{
//how should i write?
//a
virtual void func() { implementation...}

//b
void func() {implementation }
//my target is to redefine a function of ansestor
//i worry that variant b can cover ansestor function and i will not redefine it
//but what if i don't want that the function that was virtual in ansestor, will be virtual in subclass?
i'm confused
}

i don't know that to do. if i don't need this virtual function complete




You ask, "what if i don't want that the function that was virtual in ansestor, will be virtual in subclass?"


Sorry, but every function that is declared virtual in a base class, is also virtual in all derived classes.


It actually doesn't matter whether you use the virtual keyword in the derived-class declaration. Options a and b are identical -- in both cases B::func is virtual.




I suggest your write two small programs, one for each implementation to determine which suits your needs.




In C++ the function signature consists of the function name and function arguments. In a class you cannot have two functions with same signature. So your second (non-virtual) function declaration will generate compiler errors.


In short:


virtual void func() { //implementation}

and


void func() { //implementation }

have the same signature and cannot be declared in the same class.




Virtual means that reimplementation is possible in inherited class(es). Virtual function will allways be virtual no matter the depth of inheritance.



even i think that question is stupid. but i've a little experience.


i have a base class that has such method:


class A{  virtual void func(int)=0 };

and inherited class


    class  B :public A
{
//how should i write?
//a
virtual void func() { implementation...}

//b
void func() {implementation }
//my target is to redefine a function of ansestor
//i worry that variant b can cover ansestor function and i will not redefine it
//but what if i don't want that the function that was virtual in ansestor, will be virtual in subclass?
i'm confused
}

i don't know that to do. if i don't need this virtual function complete



You ask, "what if i don't want that the function that was virtual in ansestor, will be virtual in subclass?"


Sorry, but every function that is declared virtual in a base class, is also virtual in all derived classes.


It actually doesn't matter whether you use the virtual keyword in the derived-class declaration. Options a and b are identical -- in both cases B::func is virtual.



I suggest your write two small programs, one for each implementation to determine which suits your needs.



In C++ the function signature consists of the function name and function arguments. In a class you cannot have two functions with same signature. So your second (non-virtual) function declaration will generate compiler errors.


In short:


virtual void func() { //implementation}

and


void func() { //implementation }

have the same signature and cannot be declared in the same class.



Virtual means that reimplementation is possible in inherited class(es). Virtual function will allways be virtual no matter the depth of inheritance.


0 commentaires:

Enregistrer un commentaire