Puedes seguirnos en

BUSCADOR

Engineering

Rules for implementing a virtual function

When virtual functions are created for implementing late binding, there are some basic rules that satisfy the compiler requirements:

  • The virtual function must be member of some class.
  • They cannot be static members.
  • They are accessed by using object pointer.
  • The virtual function can be friend of another class.
  • A virtual function in a base class must be defined, even though it may not be used.
  • The prototype of the base class version of a virtual function and all the derived class version must be identical. If two function with same name have different prototype, C++ considers them as overloaded functions and virtual function mechanism is ignored.
  • We cannot have virtual constructor, but we can have virtual destructors.
  • While a base pointer can point to any type of the derived object, the reverse is not true. That is, we cannot use a pointer to a derived class to access an object of the base type.
  • When a base pointer points to a derived class, incrementing and decrementing it will not make it point to the next object of desired class. It is incremented or decremented only relative to its base type. Therefor, we should not use this method to move the pointer to the next object.
  • If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. In such cases, calls will invoke the base function.

Escrito por

Administrador de ENGGDRCAOS. Canal dedicado especialmente a la formación del estudiante que aspira a ser ingeniero. Todos los videos son Ingles. Apasionado del universo Apple, estudiante de Ingeniería y Gamer por vocación.

Publicidad

ARTÍCULOS RELACIONADOS

Engineering

Function prototyping is one of the key improvements added to the C++ functions. When a function call is encountered, the compiler checks the function...

Engineering

Destructors is used to destroy the objects that have been created by a constructor. The destructor is member function whose name is the same...

Engineering

Inheritance is not just simply about creating various derived classes trough a base class. There are some other benefits of using Inheritance in Object...

Engineering

The field of Computer Science revolves around writing of programs for several problems for various domains. A program consists of data structures and algorithms....