Classes and Constructors - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: Classes and Constructors

  1. #16
    Join Date
    Feb 2017
    Posts
    165

    Re: Classes and Constructors

    Quote Originally Posted by 2kaud View Post
    Whilst knowledge of separate compilation units and include-guards is important, IMO it's not something that should be introduced early into a newbie c++ course. There's plenty of other good c++ stuff that I'd teach ahead of this.
    Include guards will always be necessary because otherwise a violation of the One Definition Rule (ODR) will occur sooner or later for sure.

    The ODR is defined by the C++ language but the splitting of source code into .h and .cpp files is not. It's a convention and I don't understand why it's routinely taught to C++ newbies today. Newbies get the impression that such splitting is an important part of C++. You often see in forums like this how they proudly present meticulously split code when the only thing it accomplishes is making the code more complicated to follow.

    At least up to medium sized programs (say 1000 .h files which far exceeds a typical homework or student project) using just one translation unit is perfectly fine. I've experimented with this and I'm amazed how efficient the compiler (VS 2017 community) handles it. The compilation is very fine-grained. Each function is handled individually, both regarding need for recompilation and reevaluation of inlining.

    A typical build output can look like this (after a code change somewhere),

    Code:
    1>------ Build started: Project: mint10, Configuration: Release Win32 ------
    1>winmain.cpp
    1>Generating code
    1>191 of 6922 functions ( 2.8%) were compiled, the rest were copied from previous compilation.
    1>  0 functions were new in current compilation
    1>  16 functions had inline decision re-evaluated but remain unchanged
    1>Finished generating code
    1>mint10.vcxproj -> E:\Mint\mint10\Release\mint10.exe
    1>mint10.vcxproj -> E:\Mint\mint10\Release\mint10.pdb (Full PDB)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    The only .cpp file is winmain which holds the program entry point and the rest are include (.h) files. WTL is used but that's also include files only. The whole program is one translation unit. The build takes just a few seconds.

    The compiler has so much more information about the program than the programmer. The idea that a programmer could and should "help" the compiler doing its job is simply laughable today. Education should stop teaching C++ 89 and dated conventions.
    Last edited by wolle; July 11th, 2017 at 06:20 AM.

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

    Re: Classes and Constructors

    Include guards will always be necessary because otherwise a violation of the One Definition Rule (ODR) will occur sooner or later for sure.
    yes - and then introduce it There's also #pragma once that can be used with compilers such as VS - which IMO is more elegant if supported.

    You often see in forums like this how they proudly present meticulously split code when the only thing it accomplishes is making the code more complicated to follow.
    Yes

    Education should stop teaching C++ 89 and dated conventions.
    Definitely!

    PS and if templates are used, they can't be split between different .h and .cpp files anyhow! The STL is basically just a collection of header files.
    Last edited by 2kaud; July 11th, 2017 at 06:04 AM. Reason: PS
    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++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #18
    Join Date
    Jun 2017
    Posts
    13

    Re: Classes and Constructors

    Thank you so much 2kaud! I'll definitely read over those websites because the C++ book that I have now is nothing but confusion.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)