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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:19:52+00:00 2026-06-01T22:19:52+00:00

We define a C++ class in a .h and define its methods in a

  • 0

We define a C++ class in a .h and define its methods in a .cpp, but it makes the code look less organized.

I want to put all method’s definition in the class definition which is in a .h file, but I’m worrying that the compiler generate duplicated code for the same methods/functions when one class header file is included by different files.

Does the linker find out and merge the duplicated code pieces to reduce the file size?

If not, is it better to use .hpp instead? I heard that a .hpp is for this.

And it does make minor difference when I just change a .h file for a .hpp (I don’t know why), compiled with G++.

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-06-01T22:19:54+00:00Added an answer on June 1, 2026 at 10:19 pm

    .h vs .hpp is the 90% equivalence of

    #include <cmath> vs  #include <math.h>
    

    Some people prefer to use .hpp when they are doing exclusive C++ programming. You will see .hpp in libraries like Boost.

    However, the other 10% is really important. For example, taking from Boost library doc, they explain the reason of using .hpp over .h:

    Most Boost libraries are header-only: they consist entirely of header
    files containing templates and inline functions, and require no
    separately-compiled library binaries or special treatment when
    linking.

    If you fall in that case, you should use .hpp, but this can cost longer compilation time. Otherwise, you might want to keep .h style. That’s just my personal taste. It isn’t C-oriented at all, in my honest opinion.

    Further reading:

    Splitting templated C++ classes into .hpp/.cpp files–is it possible?

    Condensing Declaration and Implementation into an HPP file

    C++ templates declare in .h, define in .hpp

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

Sidebar

Related Questions

I want to define a class that supports __getitem__ , but does not allow
I want to define a class method that has access to a local variable.
John Nunemaker recently blogged about the various ways to define class methods in Ruby,
I have a class definition in which I want to alias several of its
I'm trying to specialize a template method of non-template class in its subclass: //
All, Why does the following code fail to compile for 'std::endl', but it's fine
When I need to get the class name inside of one of its methods
In Java, you can define an interface as a class with no actual code
I'm trying to create 2 class with the same methods name. Its an exercise,
I have some class C and want to pass address of its instance and

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.