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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:07:01+00:00 2026-06-05T19:07:01+00:00

Assume I’ve declared a function (or class, doesn’t matter) in a header file, which

  • 0

Assume I’ve declared a function (or class, doesn’t matter) in a header file, which is part of namespace foo:

namespace foo
{
    void bar();
    …
}

For a long time I’ve been reopening the namespace when I was defining the function in a cpp file:

namespace foo
{
void bar()
{
    doSomething();
    …
}
}

That is because I learned it this way and it was used in a project I was working on. I never really though about it until recently, when I stumbled upon a project which used the using directive instead:

using namespace foo;

void bar()
{
    doSomething();
    …
}

Finally there’s an option of using the full name. I find it pretty tedious, especially when classes with a lot of members are involved. In my opinion it doesn’t make much sense when all content of the file is a part of one namespace.

void foo::bar()
{
    doSomething();
    …
}

So my question is which one should be preferred and why? Especially regarding the first two options (using directive vs. reopen namespace).

  • 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-05T19:07:03+00:00Added an answer on June 5, 2026 at 7:07 pm

    I think the cleanest way is re-opening the namespace, and I’ve got the arguments to support it:

    • with your second option, with the using directive, it isn’t clear that you’re implementing a method in that namespace. You could as well be implementing a free function that uses something from the namespace.
    • the third option is usually used for implementing class member functions. If you look directly in the cpp file, it isn’t clear that you’re implementing a function from a namespace unless you know that namespace exists. The first thing that comes to mind is that you’re implementing a class member function.
    • the first one is the clearest. You open the namespace and define a function inside it. The function is part of the namespace, and this is the implementation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a class like this: public class Foo { public Bar RequiredProperty
Assume I have a Class Foo which has many internal variables, only one constructor
Assume that I have this piece of code: @interface Foo : NSObject { Bar
Assume you have a file which has been committed in your Git repo. You
Assume I have an array: $elements = array('foo', 'bar', 'tar', 'dar'); Then I want
Assume I have a class foo, and wish to use a std::map to store
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume a class Vehicle has a method called StartEngine, and in my subclass called
Assume there is a dynamic project. I have a file e.g. FileX under the
Assume I have 2 controllers that handle 2 parts of the page - header

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.