WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Advanced · question 44 of 100

What is the Pimpl idiom (pointer to implementation) in C++? Why would you use it?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

The Pimpl idiom, also known as the "pointer to implementation" or "opaque pointer" idiom, is a C++ programming technique for implementing information hiding and encapsulation. The basic idea is to hide the implementation details of a class from its users by using a pointer to a private implementation class. This allows you to change the implementation details of a class without affecting its interface or requiring users of the class to recompile their code.

To use the Pimpl idiom, you start by defining a public interface class that declares the public methods and member variables of the class. Instead of defining the implementation details of the class in the same header file, you define a private implementation class in a separate source file. The private implementation class contains the actual implementation details of the public interface class.

Here is an example of a simple class that uses the Pimpl idiom:

    // MyClass.h
    class MyClass {
        public:
        MyClass();
        ~MyClass();
        
        void doSomething();
        
        private:
        class Impl;
        Impl* m_pImpl;
    };
    
    // MyClass.cpp
    class MyClass::Impl {
        public:
        void doSomethingPrivate();
    };
    
    MyClass::MyClass() : m_pImpl(new Impl) {}
    
    MyClass::~MyClass() {
        delete m_pImpl;
    }
    
    void MyClass::doSomething() {
        m_pImpl->doSomethingPrivate();
    }
    
    void MyClass::Impl::doSomethingPrivate() {
        // implementation details here
    }

In this example, the MyClass class declares a public method doSomething(), but hides its implementation details in a private implementation class Impl. The private implementation class contains a private method doSomethingPrivate(), which is called by the public doSomething() method. The MyClass class contains a private member variable m_pImpl that points to an instance of the Impl class.

By using the Pimpl idiom, the implementation details of MyClass are hidden from the users of the class. They only need to include the header file MyClass.h, which contains the public interface of the class. The implementation details are defined in a separate source file MyClass.cpp, which can be compiled independently of the header file.

One of the main benefits of using the Pimpl idiom is that it allows you to change the implementation details of a class without affecting its interface or requiring users of the class to recompile their code. For example, you can add or remove private member variables or methods from the implementation class, or you can change the data structures used to store the data in the class, without affecting the users of the class.

Overall, the Pimpl idiom is a powerful technique for achieving information hiding and encapsulation in C++. It is widely used in modern C++ libraries and frameworks to provide a stable public interface while allowing for flexible implementation details.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics