Regular Methods (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Virtual Methods


Class methods are regular (or nonvirtual) unless you specifically declare them as virtual, or unless they override a virtual method in a base class. The compiler can determine the exact address of a regular class member at compile time. This is known as compile-time binding.

A base class regular method is inherited by derived classes. In the following example, an object of type Derived can call the method Regular() as it were its own method. Declaring a method in a derived class with the same name and parameters as a regular method in the class's ancestor replaces the ancestor's method. In the following example, when d->AnotherRegular() is called, it is being dispatched to the Derived class replacement for AnotherRegular().

class Base
{
public:
    void Regular();
    void AnotherRegular();
    virtual void Virtual();
};
class Derived : public Base
{
public:
    void AnotherRegular();          // replaces Base::AnotherRegular()
    void Virtual();                 // overrides Base::Virtual()
};
void FunctionOne()
{
  Derived *d;
  d = new Derived;
  d->Regular();                   // Calling Regular() as it were a member of Derived
                                  // The same as calling d->Base::Regular()
  d->AnotherRegular();            // Calling the redefined AnotherRegular(), ...
                                  // ... the replacement for Base::AnotherRegular()
  delete d;
}
void FunctionTwo(Base *b)
{
  b->Virtual();
  b->AnotherRegular();
}