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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:51:22+00:00 2026-05-18T19:51:22+00:00

I am used to C, where header files usually only contain declarations and not

  • 0

I am used to C, where header files usually only contain declarations and not definitions, but C++ seems to encourage the mixing of both, at least with classes. Take this class declaration which could easily be put in a header file. Some of its methods are defined inline, not in the sense of the “inline” keyword, but inline as in within the class declaration itself. Specifically the constructor and four getters/setters.

MyClass.h:

    class MyClass {
        public:
            MyClass(int a = 0, int b = 1) : _a(a), _b(b)  {};
            int getA() { return _a; };
            int getB() { return _b; };
            void setA(int a) { _a = a; };
            void setB(int b) { _b = b; };
            void doSomething(); // no definition here; defined in source file
            void doSomething2(); // no definition here; defined in source file
            void doSomething3(); // no definition here; defined in source file
        private:
            int _a;
            int _b;
    };

Is this bad form and should I define the class’s methods separately in a source file, leaving only method declarations in the class declaration, or is this perfectly acceptable?

  • 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-05-18T19:51:22+00:00Added an answer on May 18, 2026 at 7:51 pm

    It may be bad form, depending on the “bigger picture” of the applications using the header. By putting implementation into the header you’re requiring modifications to the header when that implementation changes… such modifications trigger/require client object recompilation with many build systems (e.g. make). By way of contrast, out-of-line changes may require relinking, and when shared libraries are used those libraries may be able to be replaced without any changes to the client apps. Consequently, when there’s no particular reason to use inlining, low-level headers shared by many applications tend to prefer out of line implementation. This is particularly true when the implementation can be expected to need changes.

    Another reason some people prefer separating implementation is to avoid confusing people reading the API as a form of documentation. When there’s less to read it’s generally clearer, although sometimes the implementation helps in understanding the functionality. But, programmers who see the implementation tend to think about the behavioural/performance implications and build client code that depends on that, even if the interface doesn’t make as strict guarantees: this makes the client code fragile should the implementation change.

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

Sidebar

Related Questions

header(Content-type: image/gif); readfile($filename); The above can only be used to show gif images. Is
Does anyone have a recommended way to find definitions in header files when using
I have a number of C source files(both .c and .h files). header files
how can i use POSIX header files in system programming? i used in C.
Becase I've seen (and used) situations like this: In header.h: class point { public:
I have seen below macro in many topmost header files: #define NULL 0 //
I'm pretty new to programming and am generally confused by header files and includes.
I'm trying to integrate two projects, and to that end am including header files
What's the preferred policy of header files in C++ projects: <test.h> or test.h ?
I was wondering if there was a built in runtime parser for header files

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.