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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:20:11+00:00 2026-06-16T21:20:11+00:00

I have a header file I want to include in another cpp file. I

  • 0

I have a header file I want to include in another cpp file. I want to know what is the difference if I write the header file like this,

#include <iostream>
#include <string>
using namespace std;

string ret()
{
return "called";
}

===================================

#include <iostream>
#include <string>
using namespace std;

static string ret()
{
return "called";
}

I can access the ret() function anyway!! So, what’s the use of the static?

  • 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-16T21:20:12+00:00Added an answer on June 16, 2026 at 9:20 pm

    The first header file defines a function called ret with external linkage in every translation unit that includes it. This is incorrect if more than one such TU is linked in the same program.

    The second header file defines a function called ret with internal linkage in every translation unit that includes it. This means that each TU has its own private copy of the function (with a different address) no matter how many are linked together.

    There are three correct ways to share code using a header file:

    • function with internal linkage (as in your second header, or in C++11 by putting it in a nameless namespace).
    • inline function with external linkage (replace static with inline). The meaning of inline is that although there is only one copy of the function in the program, every TU that uses the function contains its definition.
    • declare the function in the header, and define in it exactly one .cpp file (for example ret.cpp).

    In C++03 there was a fourth way:

    • function with external linkage in a nameless namespace

    I believe this is still available in C++11, but in C++11 functions in nameless namespaces have internal linkage by default. I’m not aware of any use in C++11 for making a function in a nameless namespace have external linkage. So as far as functions are concerned, nameless namespaces are a nice way of giving the function internal linkage.

    Which one you use depends on your needs. The third option means that you can change the definition of the function without re-compiling the calling code, although you’d still need to re-link the executable unless the function is in a dll.

    The first two (static or inline) differ in their behaviour if:

    • the function contains static local variables,
    • you compare function pointers to ret taken in different TUs,
    • you examine your executable size or symbol table,
    • the definition of the function is different in different TUs (perhaps due to different #defines), which is forbidden if the function has external linkage but not if internal.

    Otherwise they’re much the same.

    According to the standard, inline is also a hint that the compiler should optimize calls to that function for fast execution (which in practice means, inline the code at the call site). Most compilers ignore this hint most of the time. They will happily inline a static but non-inline function if they assess it to be a good candidate for inlining, and they will happily avoid inlining an inline function if they assess it to be a bad candidate for inlining.

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

Sidebar

Related Questions

I have a header file. In this header I want to use a map
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
Basically I have code which looks like this inside a header file: class Bar;
I want to reimplement a library of which I have the header file. I
I have this header file, zeeheader.h, and I wrote some classes in it, I'm
I have a header file named Application.h where I include a header named CollisionHandler.h.
Let's say I have a header file with a class that uses std::string .
I have a header file named A.h, another header file named B.h. In B.h,
Lets say I have a header file foo.h which declares a struct variable #include
I have one header file which uses a virtual function. This is declared 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.