Thread: Best place to forward declare PIMPL class?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Are there other practical reasons that separate this from just declaring in a header file and then implementing in a source file?
    Some practical reasons are:
    - Compile time (changing private members no longer causes a recompile in all dependent source files)
    - Hiding private members from public declarations (less "noise" in the public class declaration)
    - Binary compatibility (even if you add variables to the impl object, the size of the pointer won't change, so you won't break binary compatibility)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Was not expecting all these replies. I will add that I am learning about the PIMPL (compiler firewall) idiom to improve my knowledge of common practices. I've noticed a lot of Qt code that uses this idiom. Even if you end up working in where it's not necessary/desirable, it's good to know. You're improving your ability to parse other programmers' code mentally at the very least.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  3. #18
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    I also should add that I was able to answer one of my own questions. If the class used for the implementation object is declared locally (that is within the visible class), then it is considered a local class and has some restrictions outlined in this link. c++ - Why aren't static data members allowed in local classes? - Stack Overflow
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  4. #19
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Sorry just checking: so the client can't declare the PIMPL class in their code if they want to use it anyway? Not saying they should, just wondering if it is possible.

  5. #20
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Quote Originally Posted by jiggunjer View Post
    Sorry just checking: so the client can't declare the PIMPL class in their code if they want to use it anyway? Not saying they should, just wondering if it is possible.
    If you define/forward-declare it as a private member of the visible class, it should not be accessible through other parts of the hypothetical API / product that you are building. You might provide documentation for your own or other developers' use, but going with the general spirit of "hiding the implementation details" you would only provide information/docs about the interface (accessor functions, etc) to the common user of the product / API / whatever.

    EDIT: Also my original phrasing in the title may (?) be an abuse of terms. As far as I understand it, the term 'pimpl' comes from 'pointer to implementation', and it is a common variable name given to the pointer that points to the internal implementation class.
    Last edited by Ocifer; 07-27-2015 at 12:49 AM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC lets you forward declare a struct with "class /name/"
    By Mozza314 in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2011, 04:52 AM
  2. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 01:29 AM
  3. forward class declarations
    By manzoor in forum C++ Programming
    Replies: 17
    Last Post: 12-05-2008, 03:55 AM
  4. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  5. Forward Declaration of Class Members?
    By 691175002 in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2008, 10:34 PM