CPP in Practice - Developer's Blog

CPP in Practice - Developer's Blog

Share

Technical blog about C++!

27/02/2021

✅The override keyword was introduced in C++11. Keyword override specifies that a virtual function overrides another virtual function. If something will be wrong in derived class virtual function compile-time error will be generated.

✅For overriding to occur, several requirements must be met:
• The base class function must be virtual.
• The base and derived function names must be identical (except in case of destructors).
• The parameter types of the base and derived functions must be identical.
• The constness of the base and derived functions must be identical.
• The return types and exceptions specifications of the base and derived functions must be compatible.
• The function’s reference qualifiers must be identical.

✅So, why it is preferred to declare overriding functions as override? We can forget mark overriding function as const, pass unsigned int as parameter instead of int, or declare function as noexcept and etc… In general, the previous examples didn’t wrong. This is because we can can declare virtual function with int parameter in base class, but derived class function may accept only unsigned value and it’s expected.

ℹ Let’s discuss the following example:

class base
{
public:
virtual void f(int);
virtual void g() const;
};

class derived : public base
{
public:
virtual void f(bool);
virtual void g();
};

derived* d = new derived;
d->f(0);
d->g();

✅The code will be compiled and will work. But what if we mark derived class functions as override? In this case we can’t compile source code. This is because:
• derived::f() – parameter type mismatch. f() gets the bool parameter instead of int.
• derived::g() – specifier mismatch. g() in derived class is not const.

ℹ If we would like override derived class functions it will look like as following:

class derived : public base
{
public:
virtual void f(int) override;
void g() const override;
};

📌Note: If we are overriding function, adding “virtual” keyword is OK, but not necessary.

✅So, if we are overriding functions it is preferred to mark it as override (to avoid mistakes). But if we would like to create another virtual function in derived class which will have same name as in base class function too, in this case writing override keyword is wrong usage in this context.

📌In case of virtual function overloading that functions are not visible in derived class and if we need them in derived class we should call explicitly (Ex: call base::f()).

📖Read more about override specifier - https://en.cppreference.com/w/cpp/language/override
📖override keyword usage and advices - Scott Meyers, Effective Modern C++, Item 12: Declare overriding functions override.

05/02/2021

ℹ️Role of virtual destructors

❓We can see destructors declared as virtual very often and in some cases after some investigations we can see that destructor should not be virtual. But why? When exactly do we need to declare virtual destructors?
📌Consider, we have class Base and Derived(which is derived from Base) and Pair structure which is not designed to be base for other classes.

class Base
{
public:
Base() { std::cout

02/02/2021

ℹ Do you ever think about functions that can't return even if their return type declared void and doesn't return anything?
Example:

inline void f()
{
std::cout

01/02/2021

C was chosen as the base language for C++ because it
[1] is versatile, terse, and relatively low-level;
[2] is adequate for most systems programming tasks;
[3] runs everywhere and on everything; and
[4] fits into the UNIX programming environment.

- Bjarne Stroustrup, The C++ Programming Language Third Edition

01/02/2021

Hello!

Here you can find interesting and useful posts about C++.

Join us!

Want your school to be the top-listed School/college in Yerevan?
Click here to claim your Sponsored Listing.

Category

Website

Address


Yerevan