CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    Is it a good practice to have half implementation into headers and half to .cpp file?

    Hello!

    I'm trying to make a library in C++ in such a way that code which I don't want the client to recompile, be inside a .dll and the rest of it is implemented into the header files so that the user can Include them
    and compile them by himself. To achieve that while I'm writing the library implementation into the header files, I'm also creating a .cpp file for each header that needs to hide some of the implementation into the dll.
    The the implementation inside the header file is calling extern c functions to run the hidden implementation.

    The problem is, that after a week which I did a lot of work, now I'm getting errors use of undefined type while compiling some cpp files. I know what causes this errors but its unbelievable difficult to figure how to solve it. Let me tell you an example.

    I have Core.cpp which inludes:
    Core.h->Scene.h->Sprite.h->Animation.h->Core.h (I'm using header guards so there is no circular inclusion)

    Now, Core.cpp is trying to compile Animation.h which uses Core instances. But because inside Core.h, Animation.h is included before the definitions of Core.h this is why I'm getting the error:
    Animation.h use of undefined type Core

    The thing is that I tried to define the header guard of Animation.h inside the Core.cpp, because there is no point of compiling Animation.h inside this cpp file but this causes other problems when compiling Animation.cpp .

    I know that YOU SHOULD not actually have implementations on header files while using cpp files so I'm asking you if it's better to drop that thinking of making this library and find another way.

    Generally, there is a mess with the inclusions in my project. Everything is including stuff "from here and there". When i was making cpp aplications and I was following the method of keeping implementations inside cpps and in header files only declarations, i never had problems like that.
    Last edited by babaliaris; December 10th, 2018 at 11:10 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Is it a good practice to have half implementation into headers and half to .cpp f

    Do you mean that for example say core.h uses a class defined in animation.h and that animation.h uses a class defined in core.h?

    if so, then this is a no-no as you have a circular class reference dependency. You either need to break this dependency by introducing another class or to use a pointer in the first class definition to the other class and have a class forward reference in the first definition. Except when used as a pointer, a class can't be used until it is fully defined. Have a look at post #4 of http://forums.codeguru.com/showthrea...ke-1-arguments

    If this isn't the case, then can you provide some more info please.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2017
    Location
    Greece
    Posts
    130

    Re: Is it a good practice to have half implementation into headers and half to .cpp f

    Quote Originally Posted by 2kaud View Post
    Do you mean that for example say core.h uses a class defined in animation.h and that animation.h uses a class defined in core.h?

    if so, then this is a no-no as you have a circular class reference dependency. You either need to break this dependency by introducing another class or to use a pointer in the first class definition to the other class and have a class forward reference in the first definition. Except when used as a pointer, a class can't be used until it is fully defined. Have a look at post #4 of http://forums.codeguru.com/showthrea...ke-1-arguments

    If this isn't the case, then can you provide some more info please.
    Yea that's it. I know about Pointer To Implementation but when I was doing that I didn't thought that I will need it at that point.
    I have increased the difficulty of my project for no reason actually.

    To avoid this problem I used to follow these rules:
    1) Create declarations inside header files.
    2) Implement them inside .cpp files.
    3) Never include headers inside headers (Except standard c++ headers). Manually forward declare inside header files only classes or functions which you are interesting in and use pointer references. Then allocate and use them in the .cpp file. Now if it is very important to stack allocate them, then and only then include the header file and make sure no problems will occur.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured