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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:32:07+00:00 2026-05-11T07:32:07+00:00

I love to organize my code, so ideally I want one class per file

  • 0

I love to organize my code, so ideally I want one class per file or, when I have non-member functions, one function per file.

The reasons are:

  1. When I read the code I will always know in what file I should find a certain function or class.

  2. If it’s one class or one non-member function per header file, then I won’t include a whole mess when I include a header file.

  3. If I make a small change in a function then only that function will have to be recompiled.

However, splitting everything up into many header and many implementation files can considerately slow down compilation. In my project, most functions access a certain number of templated other library functions. So that code will be compiled over and over, once for each implementation file. Compiling my whole project currently takes 45 minutes or so on one machine. There are about 50 object files, and each one uses the same expensive-to-compile headers.

Maybe, is it acceptable to have one class (or non-member function) per header file, but putting the implementations of many or all of these functions into one implementation file, like in the following example?

// foo.h void foo(int n);  // bar.h void bar(double d);  // foobar.cpp #include <vector> void foo(int n) { std::vector<int> v; ... } void bar(double d) { std::vector<int> w; ... } 

Again, the advantage would be that I can include just the foo function or just the bar function, and compilation of the whole project will be faster because foobar.cpp is one file, so the std::vector<int> (which is just an example here for some other expensive-to-compile templated construction) has to be compiled in only once, as opposed to twice if I compiled a foo.cpp and bar.cpp separately. Of course, my reason (3) above is not valid for this scenario: After just changing foo(){…} I have to recompile the whole, potentially big, file foobar.cpp.

I’m curious what your opinions are!

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T07:32:08+00:00Added an answer on May 11, 2026 at 7:32 am

    IMHO, you should combine items into logical groupings and create your files based on that.

    When I’m writing functions, there are often a half a dozen or so that are tightly related to each other. I tend to put them together in a single header and implementation file.

    When I write classes, I usually limit myself to one heavyweight class per header and implementation file. I might add in some convenience functions or tiny helper classes.

    If I find that an implementation file is thousands of lines long, that’s usually a sign that there’s too much there and I need to break it up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have: class main { //hello } class child1 { function love($v) {
Love the JQuery Treewiev plugin http://bassistance.de/jquery-plugins/jquery-plugin-treeview/ , but I have one problem with it.
Love these forums, I am a beginner when it comes to VB.NET and have
I love jQuery. I am probably going to have some XML parsing and manipulation
I love using OneNote, however I want more control over the locations of my
Love this website! My issue is as follows: I'm reading a zip file that's
I love the new Drawing library for Google Maps v3.7, but I have the
I love Linqpad and I'm studying IronPython. Can I use Linqpad as my code
I love list comprehensions in Python, because they concisely represent a transformation of a
I love the ease that the ASP.NET Ajax control Toolkit provides to ASP.NET developers.

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.