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 5957735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:26:37+00:00 2026-05-22T18:26:37+00:00

What is the point of forwarding a class definition in a .cpp file? Imagine

  • 0

What is the point of forwarding a class definition in a .cpp file?

Imagine that I have a private class inside another public class. I forward the definition of the private class, like class Private.

Is there any advantages to putting the declaration of my private class in a .cpp file or should I just stick with my forward declaration in my public class.h and include my privateClass.h in the cpp file?

  • 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-22T18:26:38+00:00Added an answer on May 22, 2026 at 6:26 pm

    Internal classes and structs are often best kept out of public headers to avoid dependencies and coupling.

    If stuff is in the public header, it will mean that the public header will have to change if an implementation detail (in your private class/struct) changed. This is bad, because client programs will actually need to be recompiled (under the ODR – one definition rule) and various implementation defined consequences on class/vtable layout and or name mangling.

    Once you can avoid depending on the definition of your complete private type, you will avoid having all the unneeded dependencies, leading to

    1. reduced time to rebuild
    2. avoid unintended dependencies by client code on implementation private details

    A forward declared class is known as ‘incomplete type’ until it is defined (usually in a private header file or simply in the cpp file itself). Until that moment, only address-of, reference, pass by reference or pass by pointer are allowed.
    Sometimes incomplete classes can lead to tricky semantic situations; object destruction for an incomplete type will assume a non-virtual destructor (some compilers can detect this and warn if the actual definition introduces a virtual destructor). This plays a significant role when defining smart-pointers to incomplete types, e.g. in the popular pImpl idiom. Per-use the documentation of your smart pointer library (e.g. Boost SmartPtr) when in doubt.

    Update Adding backgrounder links since this is getting a popular answer:

    • Boost shared_ptr<> constructor requires complete type
    • 39.11 How can I create two classes that both know about each other?
    • 39.12 What special considerations are needed when forward declarations are used with member objects?
    • 39.13 What special considerations are needed when forward declarations are used with inline functions?
    • 39.14 Why can’t I put a forward-declared class in a std::vector<>?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a point of architectural style that I'd like your opinion on please: My ORM
I have a class set up that ideally will read the methods of any
We have a .NET application that was recently converted to .NET 4.0 and then
I have a scenario where I have single entry point Servlet and further Servlets
I have a Ruby on Rails application that I'm developing on my computer, which
I have some python module, which has a class ModuleClass and I can't modify
We have a process at our company that processes TIFF images. I have a
I have been attempting to set up magento on a shared hosting server that
Floating point precision is limited in C++ and for novice programmers that often causes
My point is that using either pod (from appy framework, which is a pain

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.