📖
Go C++
  • Introduction
  • Chapter 1: What You Must Know First
    • Virtual Address Space of Process: Memory Partition and Layout
    • Function Call: Stack Frame
    • Program Compiling and Linking
  • Chapter 2: C++ Basics Improvement
    • Default Parameters
    • Inline Function
    • Function Overloading
    • new and delete
    • const and Pointers
    • References in Detail
  • Chapter 3: Object-Oriented Principles
  • Class and Object
  • Constructor and Destructor
  • Shallow Copy and Deep Copy
  • Initializer List
  • Various Member Functions
  • Pointer to Class Members
  • Chapter 4: Template Programming
  • Function Templates
  • Class Templates
  • Memory Allocators
  • Chapter 5: Operator Overloading
    • Operator Overloading
    • Introduction to Iterators
    • Issues of Iterator Invalidation
    • More about new and delete
    • Overloading of new and delete: Object Pool
  • Chapter 6: Inheritance and Polymorphism
    • Look inside Inheritance
    • More about Inheritance
    • Virtual Functions, Static Binding and Dynamic Binding
    • More about Virtual Functions
    • Understanding Polymorphism
    • Abstract Classes
    • Frequently Asked Interview Questions: Polymorphism
  • Chapter 7: Multiple Inheritance
    • Virtual Inheritance and Virtual Base Classes
    • Diamond Problem
    • Four Kinds of Type Conversions
  • Chapter 8: Standard Template Library
    • Sequence Containers
    • Container Adaptors
    • Associative Containers
    • More about Iterators
    • Function Objects
    • Generic Algorithms, Binders and Lambda Expressions
  • Chapter 9: Object Optimization
    • Behind the Object
    • Optimizing Objects in Functions
    • Member Functions with Rvalue References
    • Move Semantics and Perfect Forwarding
  • Chapter 10: Smart Pointers
    • Smart Pointers
    • Smart Pointers without Reference Counting
    • Smart Pointers with Reference Counting
    • Custom Deleters
  • Chapter 11: Function Objects and Binders
    • More about Binders
    • Introduction to std::function
    • Template Specialization and Argument Deduction
    • More about std::function
    • std::bind(): A Simple Thread Pool
    • More about Lambda Expressions
  • Chapter 12: Multithreading
    • Important Features in C++11
    • Multithreaded Programming with std::thread
    • Mutual Exclusion
    • Producer-Consumer Problem
    • Atomic Operations
    • Thread Visibility and volatile
  • Chapter 13: Design Patterns
    • Singleton Pattern
    • Factory Pattern
    • Proxy Pattern
    • Decorator Pattern
    • Adapter Pattern
    • Observer Pattern
Powered by GitBook
On this page
  • More about virtual
  • More about Dynamic Binding

Was this helpful?

  1. Chapter 6: Inheritance and Polymorphism

More about Virtual Functions

More about virtual

Now we already learn the ability of virtual. But is there any function that cannot be implemented as virtual function? Notice that a virtual function must meet two conditions:

  1. It has an address and can be stored in the virtual function table.

  2. Its existence depends on the object.

Obviously, the constructor can not be virtual, since no object exists before its constructor is called. In this case, any function called in the constructor is a static binding. Moreover, a static member function can not be virtual, since it belongs to the class itself instead of its objects.

Is there any function that should always be virtual? Let's look at the following case:

class Base{
public:
    Base(int data) : ma(data) {
        cout << "Base()" << endl;
    }
    ~Base() {
        cout << "~Base()" << endl;
    }
protected:
    int ma;
};
​
class Derived : public Base {
public:
    Derived(int data) : Base(data), mb(data) {
        cout << "Derived()" << endl;
    }
    ~Derived() {
        cout << "~Derived()" << endl;
    }
private:
    int mb;
};

Now we use a Base pointer to point to a Derived object, and delete it after then.

int main() {
    Base *pb = new Derived(10);
    delete pb;
    return 0;
}

If we look at the output, we may find that only the destructor for class Base is called. The destructor of class Derived is not called, which will probably cause memory leak.

Base()
Derive()
~Base()

In the example above, the destructor of the base class is not marked as virtual, so there is a static binding. Good programming practice is to always marked the destructor of a base class as virtual, in which case the destructor of the derived class becomes virtual automatically.

More about Dynamic Binding

There is another question: is the call of a virtual function is always a dynamic binding? We already know that any function call in a constructor is static binding, even if the constructor is marked as virtual. Let's look at more examples.

class Base {
public:
    Base(int data = 10) : ma(data) {}
    virtual void show() {
        cout << "Base::show()" << endl;
    }
protected:
    int ma;
};
​
class Derived : public Base {
public:
    Derive(int data = 20) : Base(data), mb(data) {}
    void show() {
        cout << "Derived::show()" << endl;
    }
private:
    int mb;
}

Here we create two objects, and call the show() method directly with the object. In this case, the function calls are static binding.

int main() {
    Base b;
    Derived d;
    b.show();   // Base::show()
    d.show();   // Derived::show()
    return 0;
}

Now if we use a Base pointer to point to objects, it is a dynamic binding, no matter the object we point at is a Base object or a Derived object.

int main() {
    Base b;
    Derived d;
    Base *pb1 = &b;
    pb1->show();    // Base::show()
    Base *pb2 = &d;
    pb2->show();    // Derived::show()
    return 0;
}

References have the same underlying implementation as pointers, so it is also a dynamic binding.

int main() {
    Base b;
    Derived d;
    Base &rb1 = b;
    rb1.show(); // Base::show()
    Base &rb2 = d;
    rb2.show(); // Derived::show()
    return 0;
}

Now we know that dynamic binding is a way to achieve polymorphism. Therefore, only with pointers and references is the function call a dynamic binding.

PreviousVirtual Functions, Static Binding and Dynamic BindingNextUnderstanding Polymorphism

Last updated 4 years ago

Was this helpful?