Home > Backend Development > C++ > body text

How to Create Copies of Derived Class Instances from Base Class Pointers in C ?

Patricia Arquette
Release: 2024-10-26 04:55:31
Original
358 people have browsed it

How to Create Copies of Derived Class Instances from Base Class Pointers in C  ?

Polymorphism: Creating Copies of Derived Class Instances from Base Class Pointers

In object-oriented programming, it's common to encounter scenarios where you need to create copies of derived class instances while accessing them through pointers to their polymorphic base class. This task can be challenging, especially when avoiding the use of external libraries or complex code structures.

The Problem:

Consider the following class hierarchy:

<code class="cpp">class Base {
  virtual ~Base();
};
class Derived1 : public Base { ... };
class Derived2 : public Base { ... };</code>
Copy after login

The objective is to create a method, CreateCopy(Base* base), that returns a dynamically created copy of the instance pointed to by base. It's crucial to avoid returning a reference to a temporary object to prevent runtime errors.

The Naive Approach:

A naive approach would involve using multiple typeids or dynamic_casts in a series of if-statements to determine the specific derived class type and then utilize the new operator to create a copy. However, this method is cumbersome and error-prone.

A Better Approach:

A more elegant and versatile solution is to introduce a virtual method in the base class:

<code class="cpp">class Base {
  virtual ~Base();
  virtual Base* clone() const = 0;
};</code>
Copy after login

Each derived class then implements its own clone() method to create a copy of the instance.

CRTP (Curiously Recurring Template Pattern):

To avoid duplicating the clone() implementation in each derived class, you can leverage the CRTP idiom through a template class:

<code class="cpp">template <class Derived>
class DerivationHelper : public Base {
public:
  virtual Base* clone() const {
    return new Derived(static_cast<const Derived&>(*this)); // call the copy ctor.
  }
};</code>
Copy after login

Now, derived classes can inherit from DerivationHelper instead of directly from Base:

<code class="cpp">class Derived1 : public DerivationHelper<Derived1> { ... };
class Derived2 : public DerivationHelper<Derived2> { ... };</code>
Copy after login

This approach ensures that all derived classes have access to the clone() method without the need for manual implementation in each class.

The above is the detailed content of How to Create Copies of Derived Class Instances from Base Class Pointers in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!