Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a pure virtual function inherited from base class?

Let's say I have a base class called Human. Baby and Adult are sub-classes of Human. Human has a function run as a pure virtual function and I don't want Baby to inherit run but if I don't override run, Baby would become an abstract class.

class Human{
 public:
   // Some human attributes.
   virtual void run()=0;
};

class Adult: public Human{
 public:
   void run(){
      // Adult running function
   }
};

class Baby: public Human{
 public:
   void run()=delete; //error

   // If I don't override `run` then Baby is marked as an abstract class

   void crawl(){
     // Baby crawling function.
   }
};

Error if I mark run with =delete

prog.cpp:19:10: error: deleted function ‘virtual void Baby::run()’
     void run()=delete;
          ^~~
prog.cpp:6:22: error: overriding non-deleted function ‘virtual void Human::run()’
         virtual void run()=0;
                      ^~~

Might be a bad example but what I want is to inherit everything except for run in Baby. Is it possible?


Disclaimer: I'm not implementing it anywhere. Just out of curiosity.

like image 929
Ch3steR Avatar asked Jan 15 '21 06:01

Ch3steR


People also ask

Can a pure virtual function be inherited?

Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class. The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f() from AB .

Do pure virtual functions have to be overridden?

¶ Δ A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.

Does a derived class inherit virtual functions?

Derived classes do not have to implement all virtual functions themselves. They only need to implement the pure ones. That means the Derived class in the question is correct. It inherits the bar implementation from its ancestor class, Abstract .

Is a pure virtual function is defined in the base class?

A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.

How to declare a pure virtual function in Java?

A pure virtual function is declared by assigning 0 in declaration. See the following example. A pure virtual function is implemented by classes which are derived from a Abstract class. Following is a simple example to demonstrate the same. 1) A class is abstract if it has at least one pure virtual function.

What are pure virtual functions and abstract classes in C++?

Pure Virtual Functions and Abstract Classes in C++. Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class.

What happens when a derived class overrides a virtual function?

Once a function is declared as virtual, it stays virtual no matter how many layers of derived classes it may pass through. When a derived class does not override a virtual function, then the function, as defined in the base class, is used. For example, try this version of the preceding program in which Derived_Class2 doesn't override who ().

Can We define a pure virtual function with a body?

It turns out that we can define pure virtual functions that have bodies: In this case, speak () is still considered a pure virtual function because of the “=0” (even though it has been given a body) and Animal is still considered an abstract base class (and thus can’t be instantiated).


1 Answers

This is not possible. Consider the following code:

Human *CreateAPerson() {
  return new Baby();
}

int main() {
  Human *human = CreateAPerson();
  human->run();
}

The compiler would have no way to know that human->run() is illegal.

like image 75
Bill Lynch Avatar answered Sep 20 '22 14:09

Bill Lynch