Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8016563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:34:19+00:00 2026-06-04T20:34:19+00:00

The PIMPL Idiom is a technique for implementation hiding in which a public class

  • 0

The PIMPL Idiom is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of. This hides internal implementation details and data from the user of the library.

But is it possible to implement the same making use of reference?

MCanvasFont.h

namespace Impl {
    class FontDelegate;
}

class MCanvasFont
{
public:
    MCanvasFont();
    virtual ~MCanvasFont();

protected:
    // Reference count
    long m_cRef;

    // agg font delegate
    const Impl::FontDelegate& m_font;
}

MCanvasFont.cpp

// helpers
#include "ImplHelpers/FontDelegate.h"

MCanvasFont::MCanvasFont()
: m_cRef(1),
  m_font(Impl::FontDelegate() )
{
    // constructor's body
}

P.S. This code compiles without any problems with G++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:34:20+00:00Added an answer on June 4, 2026 at 8:34 pm

    There is an error in your program, and it’s in the constructor’s initialiser list:

    MCanvasFont::MCanvasFont()
    : m_cRef(1),
      m_font(Impl::FontDelegate() ) // <--- BANG
    {
    

    The problem with Impl::FontDelegate() is that it constructs a temporary object. This won’t outlive the constructor – in fact it’s actually destroyed before entering the constructor body, as it’s lifetime is that of the expression in which it appears. Thus your m_font reference is instantly invalid.

    While you could initialise it using a manually allocated object (*new Impl::FontDelegate()) you’re in undefined territory if that allocation fails unless you have exceptions enabled in your runtime. You’ll also still have to delete the object in your destructor anyway. So the reference really doesn’t buy you any advantages, it just makes for some rather unnatural code. I recommend using a const pointer instead:

    const Impl::FontDelegate* const m_font;
    

    EDIT: Just to illustrate the problem, take this equivalent example:

    #include <iostream>
    
    struct B
    {
        B() { std::cout << "B constructed\n"; }
        ~B() { std::cout << "B destroyed\n"; }
    };
    
    struct A
    {
        const B& b;
        A() :
            b(B())
        {
            std::cout << "A constructed\n";
        }
        void Foo()
        {
            std::cout << "A::Foo()\n";
        }
        ~A()
        {
            std::cout << "A destroyed\n";
        }
    };
    
    int main()
    {
        A a;
        a.Foo();
    }
    

    If you run this, the output will be:

    B constructed
    B destroyed
    A constructed
    A::Foo()
    A destroyed
    

    So b is invalid almost immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Backgrounder: The PIMPL Idiom (Pointer to IMPLementation) is a technique for implementation hiding in
I'm trying to use pimpl idiom. In particular, the implementation class would implement another
I'm using the pimpl-idiom with std::unique_ptr : class window { window(const rectangle& rect); private:
The pImpl idiom in c++ aims to hide the implementation details (=private members) of
I'm playing around with creating a utility class for the pimpl idiom, however I
I am trying to use the pimpl pattern and define the implementation class in
I have three classes: A data holder class CDataHolder, which uses a Pimpl pattern
It is my understanding that the primary benefit of the pimpl idiom is to
I have a wrapper class that delegates its work to a pimpl, and the
I just noticed a new term pimpl idiom, what's the difference between this idiom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.