How declare part of class definition in header and the rest in class file?

This is a discussion on How declare part of class definition in header and the rest in class file? within the C++ Programming forums, part of the General Programming Boards category; I want to declare only a few functions of a class' signature in a header file and then do the ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How declare part of class definition in header and the rest in class file?

    I want to declare only a few functions of a class' signature in a header file and then do the rest of the class (including implementation of those declared functions) elsewhere. How do I do this?

    Code:
    class Test
    {
        void doIt();
    };
    
    //Finish def.
    class Test
    {
         void  doIt(){...}
         void aNewFunction() {...}
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Use the pimpl idiom, also known as compiler firewall.

    In some cases you can also just make helper functions in the unnamed namespace of the source file that aren't part of the class.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Code:
    void Test::doIt()
    {
    }
    return Class::Function()

    Simple.
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like Daved says, you need a "visible" driver class and an "invisible" implementation class. Something like this:
    Code:
    // class.h
    class impl;  // Forward declaration
    
    class driver
    {
       private: 
          impl *pImpl;
          void setImpl(const impl *aPImpl) { pImpl = aPImpl; }
          void doSomething();
       public:
          void doit();
          ...
    };
    
    // impl.h
    class impl
    {
        private:
           int x; 
           // stuff goes here. 
        public:
           doSomething() { x++; }
    };
    
    // class.cpp
    #include "impl.h"
    void driver::doSomething() 
    {
        pImpl->doSomething;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Code:
        pImpl->doSomething;
    You sure that's not
    Code:
        pImpl->doSomething();
    ?
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Code:
        pImpl->doSomething;
    You sure that's not
    Code:
        pImpl->doSomething();
    ?
    Sorry, not quite awake yet, only on my first dose of caffeine ;-)

    You are right, there should be () on the end there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21