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

  • Home
  • SEARCH
  • 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 188585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:05:30+00:00 2026-05-11T16:05:30+00:00

Is it possible to make a C++ header file (.h) that declares a class,

  • 0

Is it possible to make a C++ header file (.h) that declares a class, and its public methods, but does not define the private members in that class? I found a few pages that say you should declare the class and all its members in the header file, then define the methods separately in you cpp file. I ask because I want to have a class that is defined in a Win32 DLL, and I want it to be properly encapsulated: the internal implementation of that class might change, including its members, but these changes should not affect code that uses the class.

I guess that if I had this, then it would make it impossible for the compiler to know the size of my objects ahead of time. But that should be fine, as long as the compiler is smart enough to use the constructor and just pass around pointers to the location in memory where my object is stored, and never let me run “sizeof(MyClass)”.

Update: Thanks to everyone who answered! It seems like the pimpl idiom is a good way to achieve what I was talking about. I’m going to do something similar:

My Win32 DLL file will have a bunch of separate functions like this:

void * __stdcall DogCreate();
int __stdcall DogGetWeight(void * this);
void __stdcall DogSetWeight(void * this, int weight);

This is the typical way the Microsoft writes their DLL files so I think there is probably good reason for it.

But I want to take advantage of the nice syntax C++ has for classes, so I’ll write a wrapper class to wrap up all of these functions. It will have one member, which will be “void * pimpl”. This wrapper class will be so simple that I might as well just declare it AND define it in the header file. But this wrapper class really has no purposes other than making the C++ code look pretty as far as I can tell.

  • 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-05-11T16:05:30+00:00Added an answer on May 11, 2026 at 4:05 pm

    I think what you are looking for is something called the “pimpl idiom”. To understand how this works, you need to understand that in C++ you can forward declare something like so.

    class CWidget; // Widget will exist sometime in the future
    CWidget* aWidget;  // An address (integer) to something that 
                       // isn't defined *yet*
    
    // later on define CWidget to be something concrete
    class CWidget
    {
         // methods and such 
    };
    

    So to forward declare means to promise to fully declare a type later. Its saying “there will be this thing called a CWidget, I promise. I’ll tell you more about it later.”.

    The rules of forward declaration say that you can define a pointer or a reference to something that has been forward declared. This is because pointers and references are really just addresses-a number where this yet-to-be-defined thing will be. Being able to declare a pointer to something without fully declaring it is convenient for a lot of reasons.

    Its useful here because you can use this to hide some of the internals of a class using the “pimpl” method. Pimpl means “pointer to implementation”. So instead of “widget” you have a class that is the actual implementation. The class you are declaring in your header is just a pass-through to the CImpl class. Here’s how it works:

    // Thing.h
    
    class CThing
    {
    public:
        // CThings methods and constructors...
        CThing();
        void DoSomething();
        int GetSomething();
        ~CThing();
    private:
        // CThing store's a pointer to some implementation class to 
        // be defined later
        class CImpl;      // forward declaration to CImpl
        CImpl* m_pimpl;  // pointer to my implementation
    };
    

    Thing.cpp has CThing’s methods defined as pass-throughs to the impl:

    // Fully define Impl
    class CThing::CImpl
    {
    private:
         // all  variables
    public:
         // methods inlined
         CImpl()
         {
              // constructor
         }
    
         void DoSomething()
         {
              // actual code that does something
         }
         //etc for all methods     
    };
    
    // CThing methods are just pass-throughs
    CThing::CThing() : m_pimpl(new CThing::CImpl());
    {
    }  
    
    CThing::~CThing()
    {
        delete m_pimpl;
    }
    
    int CThing::GetSomething()
    {
        return m_pimpl->GetSomething();
    }
    
    void CThing::DoSomething()
    {
        m_impl->DoSomething();
    }
    

    tada! You’ve hidden all the details in your cpp and your header file is a very tidy list of methods. Its a great thing. The only thing you might see different from the template above is that people may use boost::shared_ptr<> or other smart pointer for the impl. Something that deletes itself.

    Also, keep in mind this method comes with some annoyances. Debugging can be a tad bit annoying (extra level of redirection to step through). Its also a lot of overhead for creating a class. If you do this for every class, you’ll get tired of all the typing :).

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

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In this particular case you don't need to; you can… May 12, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer We are using IIS 7 hosting about 20 services with… May 12, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer You can avoid a useless use of cat and handle… May 12, 2026 at 8:42 pm

Related Questions

Most of my C/C++ development involves monolithic module files and absolutely no classes whatsoever,
I'm new to C++, so this question may be basic: I have two classes
Possible Duplicates: C/C++: Detecting superfluous #includes? How should I detect unnecessary #include files in
I want to design & implement a simple windowing & widget for OpenGL running

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.