Tebe
Tebe

Reputation: 3214

c++ virtual function reimplementation

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

Upvotes: 1

Views: 942

Answers (4)

Andro
Andro

Reputation: 2232

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

Upvotes: 1

user258808
user258808

Reputation: 734

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.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168616

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.

Upvotes: 4

levis501
levis501

Reputation: 4207

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

Upvotes: 2

Related Questions