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

  • Home
  • SEARCH
  • 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 8479495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:02:30+00:00 2026-06-10T19:02:30+00:00

Possible Duplicate: Inline functions in C++ What does the compiler do if I completely

  • 0

Possible Duplicate:
Inline functions in C++

What does the compiler do if I completely implement a class in its header file? A typical example follows:

class MyException
{    
public:
    explicit MyException(const char* file, int line) file(file), line(line) {};
    const char* getFile() const { return file };
    int getLine() const { return line };
private:
    const char* const file;
    const int line;
};

My intention is to use the class like this: throw MyException(__FILE__, __LINE__).

I include this header file into each .cpp file. I suppose the compiler will compile the class as many times as it is defined and include the (identical) machine code into every object file it produces. Now, what will the linker do? I tried a simpler example (without all those pesky const‘s) and it compiled fine.

What would happen, if instead of a simple class, I implemented a three-screenful-long C function in a header file? And the final question, should I split my example into .h and .cpp files?

  • 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-10T19:02:32+00:00Added an answer on June 10, 2026 at 7:02 pm

    A class definition itself doesn’t produce any code. It just shows users of the class how it is layed out, so they can generate appropriate code to manipulate it.

    It’s the member functions of the class that generate code. When you define a member function inside the class definition it gives the function an implicit inline declaration.

    A function call can be compiled and linked in one of two ways:

    (1) A single copy of the function code with a RETURN assembly instruction at the end can be placed in the image, and a CALL assembly instruction can be placed (along with param passing and return value transfer) at the call site to transfer control to this code.

    or

    (2) An entire copy of the function implementation can replace the entire function call at the call site.

    A function declared inline is a recommendation to the compiler to do it the second way. Further an inline declaration allows the function to be defined in several translation units (so it can be placed in a shared header file). In order for the compiler have the option of implementing the second method, it needs a copy of the function implementation at compile-time. This isn’t available if the function implementation is in a foreign translation unit.

    It should also be noted that modern compilers do complicated things with functions declared inline. See:

    http://gcc.gnu.org/onlinedocs/gcc/Inline.html

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

Sidebar

Related Questions

Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicate: Inline functions in C#? What is method inlining? i've been debugging code,
Possible Duplicate: Benefits of inline functions in C++? I have a confusion regarding the
Possible Duplicate: Inline functions vs Preprocessor macros Inline functions were introduced in C++ to
Possible Duplicate: Including PHP functions from another file I'm building code that use function
Possible Duplicate: Why can templates only be implemented in the header file? I realise
Possible Duplicate: Inline functions in C++ Modern compilers are better than programmers at deciding
Possible Duplicate: Fix for Xcode's indiscernible highlighting of inline errors? Does anyone know how
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate

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.