c++ - Calling functions within namespaces -


is possible call function within namespace? need because function assigns meaningful values variables. if not, there workaround? idea of want below, foo function within variables assigned values, , bar1, bar2 2 variables given values.

namespace space{     bar *bar1;     bar *bar2;      void foo(bar *b1, bar *b2) {/*b1 , b2 , given values*/}     foo(bar1, bar2); // foo function call. } 

to clarify, bar1 , bar2 should defined first time called , no other time.

this does, believe, op wants, it's horrible. every cpp file includes space.h going instantiate bar's 1 , 2 , init. naming collision hell when linker tries sort out.

#ifndef space_h #define space_h #include <iostream> namespace space {     class bar     {     public:         // use own stuff here. sample use only.         bar(const std::string & str):mstr(str)         {          }         friend std::ostream &operator<<(std::ostream & out, const bar &bq)         {             out << bq.mstr;             return out;         }     private:         std::string mstr;     };     bar *bar1; // consider using std::unique_ptr in place of raw pointer     bar *bar2;      class init     {     public:         init()         {             bar1 = new bar("bar, bar, bar");             bar2 = new bar("barbara anne");         }         virtual ~init() // std::unique_ptr makes destructor unnecessary         {              delete bar1;              delete bar2;         }     }     init init; // init construct , assign bars before main                 // , destruct , delete bars when program exits } #endif 

static makes marginally better static restricts each bar , init each including cpp file, have variables duplicated in each including cpp file, more ram use , changing 1 not change others.

    static bar *bar1;     static bar *bar2;      class init     {     public:         init()         {             bar1 = new bar("bar, bar, bar");             bar2 = new bar("barbara anne");         }         virtual ~init()         {              delete bar1;              delete bar2;         }     };     static init init; 

another tweak doesn't op wants, it's close, lot safer take 1, , unified across compilation units unlike take 2. extern instructs compiler allow use of bars 1 , 2 without instantiating them, means has allocate space them.

    extern bar *bar1;     extern bar *bar2;      class init     {     public:         init()         {             bar1 = new bar("bar, bar, bar");             bar2 = new bar("barbara anne");         }         virtual ~init()         {              delete bar1;              delete bar2;         }     }; 

and main cpp demonstrate use

#include <iostream> #include "space.h"  // allocate bars , init object space::bar *space::bar1;  space::bar *space::bar2;  space::init init;  int main() {     std::cout << *space::bar1 << std::endl;     std::cout << *space::bar2 << std::endl;     return 0; } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -