Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception specification when overriding a virtual function?

Tags:

c++

If a class inherits from another and overrides a virtual function, how must the exception-specification for the derived (overridden) function be related to the base function?

Is that is must be at least as restrictive? right? or others?

like image 630
Josh Morrison Avatar asked Mar 14 '11 05:03

Josh Morrison


People also ask

Can you override a virtual function?

Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual . The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.

What happens if we don override virtual function in inheritance?

If you don't override a pure virtual function in a derived class, that derived class becomes abstract: class D2 : public Base { // no f1: fine.

Do I need to override virtual functions?

It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. A class may have virtual destructor but it cannot have a virtual constructor.

What is an exception specification when is it used?

Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated by a function. You can specify that a function may or may not exit by an exception by using an exception specification.


1 Answers

Yes, it must be at least as restrictive (§15.4/3):

If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function.

like image 147
Jerry Coffin Avatar answered Sep 18 '22 22:09

Jerry Coffin