C++ vs Rust — simple polymorphism comparison

C++ vs Rust — simple polymorphism comparison

In this short article, we’ll compare and contrast how C++ and Rust handle dynamic polymorphism. A quick disclaimer, I’m not a Rust guru, but rather someone who’s more familiar with C++ (by the way, you can join our San Diego C++ meetup at

This is my first Medium.com story. After reading, tweeting, and bookmarking hundreds of stories, I’ve decided to create a simple one for those of you learning Rust after coming from C++, just like me.

In this short article, we’ll compare and contrast how C++ and Rust handle dynamic polymorphism. A quick disclaimer, I’m not a Rust guru, but rather someone who’s more familiar with C++ (by the way, you can join our San Diego C++ meetup at https://www.meetup.com/San-Diego-CPP/ )

Let’s start with the purpose of polymorphism: with dynamic, runtime polymorphism, we’re able to hold a pointer or reference to a type that is actually pointing to a more concrete, derived type. It’s the basic lesson that people learn when working with OO (Object Oriented) languages. For our example, we’ll use Animal *as the base, top level type, along with more concrete types, like *Cat and Dog.

Animal class type has a

  1. Virtual default destructor. This is needed to deallocate the correct type when dealing with heap allocation. Although this is outside of the scope of this article, feel free to ask questions in the comments section.
  2. Virtual method/member function talk() . It is pure virtual, not implemented. The derived, concrete class(es) type(s) must implement it in order to create an instance. Here is how:

The above is simple: we create two newclasses, Dog and Cat, use public inheritance from Animal, and implement the pure virtual function declared in the Animal *base class, *talk(). *The *final *keyword defines that the class will be a leaf type. You cannot subclass from *Dog/Cat.

Here is how we can use the class hierarchy demonstrated in “Modern C++”.

For this to compile, you will need to #include *the following headers: *,**  and **.

In the above main function, we created a vector of pointers to the Animal *class type. We use *unique_ptr<> to manage the heap allocation and deallocation. This is a C++11 feature. make_unique<>() is a helper function from C++14 that allocates such concrete type instances.

Finally, we create the loop (C++11 feature — range for loop) to iterate over Animal and invoke talk() on each instance. So long as each type implements the talk() interface (override virtual function of *Animal *base class), the type of the concrete instance is irrelevant.

object-oriented cpp rust

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Creating RESTful APIs with NodeJS and MongoDB Tutorial - Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.

CentOS Linux 8.2 Released and Here is How to Upgrade it

CentOS Linux 8.2 (2004) released. This release adds corrections for security issues based upon RHEL 8.2 souce and here is how to upgrade it.