Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Globals and namespace

Globals and namespace

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
6 Posts 6 Posters 0 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.
  • K Offline
    K Offline
    karmendra_js
    wrote on last edited by
    #1

    I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

    T C L T A 5 Replies Last reply
    0
    • K karmendra_js

      I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      yes, you can put them in a namespace, and it's good style to do so. to declare:

      namespace myNameSpace
      {
      bool globalBool;
      CMyObject globalObject;
      ...
      };

      ... to use:

      myNameSpace::globalBool = false;

      Cleek | Image Toolkits | Thumbnail maker

      1 Reply Last reply
      0
      • K karmendra_js

        I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        namespace MyNameSpace {
        //declare here whatever you want...
        }


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        1 Reply Last reply
        0
        • K karmendra_js

          I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Just a little addition to previous replies , if you don't want to write myNameSpace::SomeFunction() every time you call it, then you can add following line to your main() function : using namespace myNameSpace; And then you will be able to directly call SomeFunction() in main();


          "Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill

          1 Reply Last reply
          0
          • K karmendra_js

            I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            karmendra_js wrote: Also I don't know how to create and use namespace.

            Name Space[^]

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV

            1 Reply Last reply
            0
            • K karmendra_js

              I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required. I want to know Can i use nampespace to store these globals and is it of any importance. Also I don't know how to create and use namespace. Can you help me plz.

              A Offline
              A Offline
              Arman S
              wrote on last edited by
              #6

              Hi, Yes you can locate global variables in namespaces. The importance of doing so depends on the specific case. For example, if you are afraid your var's name could conflict with another var's name, put yours in a namespace. Another example, if you have many global variables (hope you don't) you'd rather put them in a namespace in terms of 'well organization'. Code: namespace Globals { bool bVar1; bool bVar2; // any others... } // namespace Globals Now let's use it: // method 1. (not preferred) using namespace Globals; void f() { bVar1 = false; } // method 2. (less preferred) using Global::bVar1 void f() { bVar1 = false; } // method 3. (ok) void f() { Globals::bVar1 = false; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups