4

I was looking at several SO posts (listed below) regarding the benefit of using base class pointer to a derived class object. But in most of the cases, virtual function is the ultimate purpose of using base class pointer. I added comments to some of these posts, but I guess since the posts are old, responses were not received. Now my question is, if we do not use virtual function concept, are there any purpose of using base class pointer in some other case?

In the code below,

pEmp->computeTotalSalary

calls base class function since virtual function is missing. This is expected behavior.

#include <iostream>
using namespace std;
class Employee
{
public:

    void computeTotalSalary ()
    {
        cout<<"Computing Employee Salary in Base Class:"<<endl;
    }
};

class TeachingStaff : public Employee
{
public:
    // Override the function

    void computeTotalSalary ()
    {
        cout<<"Computing Teacher's Salary in Sub class: "<<endl;
    }
};

int main()
{
    TeachingStaff *pTeacher = new TeachingStaff() ;
    pTeacher->computeTotalSalary();
    Employee *pEmp;
    pEmp = pTeacher;
    pEmp->computeTotalSalary();
    pTeacher->Employee::computeTotalSalary();
}

Links: 1. Why use base class pointers for derived classes

  1. A Base Class pointer can point to a derived class object. Why is the vice-versa not true?

  2. assigning derived class pointer to base class pointer in C++

Rajesh
  • 1,085
  • 1
  • 12
  • 25

2 Answers2

4

Using a base class pointer or reference allows you to write functions that accept all derived classes of the base generically so you can call common non-virtual functions on them. Otherwise, you'd have to write the same function multiple times for the different derived classes, or use templates.

#include <iostream>

class base_t {
public:
    void non_virtual() const { std::cout << "Hello, World!\n"; }
};

class derived_1_t : public base_t { };
class derived_2_t : public base_t { };

void do_something(const base_t& obj) {
    obj.non_virtual();
}

int main() {
    derived_1_t var1;
    derived_2_t var2;
    do_something(var1);
    do_something(var2);
}
Jonesinator
  • 4,186
  • 2
  • 24
  • 18
1

While you can intentionally hide functions from the base class, it is most often an unintentional mistake (or poor design) to do so. The C++11 override keyword was introduced to help prevent this mistake.

There are other use cases for inheritance beyond virtual functions and polymorphism. For instance: (1) one might put a bunch of objects in a heterogeneous container such as std::vector<Employee*> where it doesn't need access to the derived class data, though this is probably suspect without polymorphism also, (2) inheriting behavior or POD data members (also questionable practice), or (3) inheriting for policy-based design (see Alexandrescu's Modern C++ Design).

Generally, you also want a destructor for inheritance roots. Herb Sutter says, "A base class destructor should be either public and virtual, or protected and nonvirtual."

You might also consider not using inheritance in this way. See "Better Code: Runtime Polymorphism" by Sean Parent, which may just blow your mind.

metal
  • 6,202
  • 1
  • 34
  • 49