Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Namespace best practice
Forum Updated to NodeBB v4.3 + New Features

Namespace best practice

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    Hi all,
    I'm going to refactor a few classes in my project to belong all to the same namespace. Each class has its own header and implementation file. I'm just curious to know which is the best practice to contain all of them in a single namespace, do I have to create a "namespace" file like:

    @
    namespace mySpace{
    class MyClassA;
    class MyClassB;
    }@

    or add the namespace block in each header file or what? Any suggestion is welcome.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AlekseyOk
      wrote on last edited by
      #2

      Namespaces allows you to combine a set of similar classes (or not similar classes but library - remember namespace std) into one group.

      I think it's not very useful to put each class into separate namespace...

      Of course namespace can help you with potential ambiguity in different classes/files..

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        [quote author="AlekseyOk" date="1337850487"]
        I think it's not very useful to put each class into separate namespace...
        [/quote]

        I don't want to use a separate namespace for each class, I want to put all classes into a single namespace.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AlekseyOk
          wrote on last edited by
          #4

          To add all classes into one namespace you should add

          @
          namespace youSpace
          {
          you_class_declaration and/or definition
          }
          @

          to each header and cpp file. Of course you can add

          @
          using namespace youSpace;
          @

          to cpp file.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved