Ask Your Question
1

What is the method for utilizing Pimpl to attain a base class with virtual functions that is templated in C++17?

asked 2023-07-10 21:55:53 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-10 22:01:01 +0000

djk gravatar image

To utilize Pimpl to attain a base class with virtual functions that is templated in C++17, follow these steps:

  1. Define the public interface of the base class with virtual functions inside a template class.

  2. Declare a private Pimpl (Pointer to Implementation) class inside the template class. The Pimpl class will contain the private data members and implementation details of the base class.

  3. Define the Pimpl class inside the template class, and move the private data members and implementation details of the base class into the Pimpl class. The Pimpl class should not have any virtual functions.

  4. Define the virtual functions of the base class as pure virtual functions inside the template class, and delegate their implementation to the Pimpl class.

  5. Implement the virtual functions of the base class outside the template class, and use the Pimpl class to access the private data members and implementation details.

  6. Instantiate the template class with the desired template arguments to create an instance of the base class.

Here is an example code:

template <typename T>
class Base {
public:
    virtual void foo() = 0;
    virtual void bar() = 0;
    virtual ~Base() = default;

private:
    class Pimpl;
    Pimpl *pimpl;
};

template <typename T>
class Base<T>::Pimpl {
    // private data members and implementation details
};

template <typename T>
class Derived : public Base<T> {
public:
    void foo() override {
        this->pimpl->foo_impl(); // delegate to Pimpl
    }
    void bar() override {
        this->pimpl->bar_impl(); // delegate to Pimpl
    }
};

template <typename T>
void Derived<T>::foo_impl() {
    // implementation using Pimpl
}

template <typename T>
void Derived<T>::bar_impl() {
    // implementation using Pimpl
}

int main() {
    Derived<int> d; // creates an instance of the base class
    d.foo();
    d.bar();
    return 0;
}

In this example, the base class Base is a template class with two pure virtual functions foo and bar. The private Pimpl class Pimpl contains the private data members and implementation details, and is declared inside the template Base class. The derived class Derived overrides the virtual functions foo and bar, and delegates their implementation to the Pimpl class through the pimpl pointer. The implementation of the virtual functions foo and bar using the Pimpl class is defined outside the template Derived class. Finally, an instance d of the Derived class is created using the template argument int.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-10 21:55:53 +0000

Seen: 11 times

Last updated: Jul 10 '23