【问题标题】:Calling functions within namespaces在命名空间内调用函数
【发布时间】:2015-07-18 02:58:07
【问题描述】:

是否可以在命名空间内调用函数?我需要这样做,因为该函数为变量分配了有意义的值。如果没有,是否有解决方法?下面是我想要的一个想法,foo 是为变量赋值的函数,bar1bar2 是要为两个变量赋值的函数。

namespace space{
    bar *bar1;
    bar *bar2;

    void foo(bar *b1, bar *b2) {/*b1 and b2 and given values*/}
    foo(bar1, bar2); // this foo is a function call.
}

为了澄清,bar1 和 bar2 应该只在它们第一次被调用时定义,其他时候不能。

【问题讨论】:

  • 我不确定您所说的“在命名空间中调用函数”是什么意思...也许这是对the question you asked 2 hours ago 的改写?
  • @DrewDormann 关闭但不完全正确。另一种需要单例模式。我的实际代码(未显示)已经在一个类中完成了这一点。现在我想将一个命名空间变量设置为单例值(实际上是两个变量,单例对),以便每当用户希望访问该变量时,他或她都可以通过命名空间而不是通过类来访问。我希望这个例子能澄清。有没有办法通过函数来​​定义命名空间变量的值。
  • 在你的程序执行过程中,什么时候需要运行这个函数?
  • C++ 不直接支持。最接近的可能是每当有人通过覆盖operator-> 的“智能容器”类访问对象时执行函数。我会给你一个完整的答案,但我要在全麦面包上吃一些很棒的培根包裹的狗。
  • 我认为您对 SO 有多大帮助的评估不符合实际。德鲁花时间澄清你的帖子,有人提供了答案,当那些多汁多汁的热狗像角质舞会约会一样喊我的名字时,我提出了一个高水平的建议。您的帖子不清楚,虽然可以挽救,但缺乏明确性限制了响应。尽管您在 cmets 中提供了一些说明,但应将这些 cmets 移至您的帖子中。这肯定会让我更倾向于根据我之前的评论花时间提供一个可能可接受的解决方案。 600 个字符!!

标签: c++ function namespaces call


【解决方案1】:

我相信,这正是 OP 想要的,但这太可怕了。每个包含 space.h 的 CPP 文件都将实例化 bar 的 1 和 2 并初始化。当链接器尝试将其全部整理出来时,将其命名为冲突地狱。

#ifndef SPACE_H
#define SPACE_H
#include <iostream>
namespace space
{
    class bar
    {
    public:
        // use your own stuff here. This is for 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 the 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 this destructor unnecessary
        {
             delete bar1;
             delete bar2;
        }
    }
    Init init; // init will construct and assign the bars before main 
               // and destruct and delete the bars when the program exits
}
#endif

static 稍微好一点 static 将每个 bar 和 init 限制为每个包含 CPP 文件,但是现在您在每个包含 CPP 文件中都有重复的变量,更多的 RAM 使用和更改一个不会改变其他的。

    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;

另一个调整并不完全符合 OP 的要求,但它很接近,比 take 1 更安全,并且与 take 2 不同,在编译单元之间统一。extern 指示编译器允许在不实例化的情况下使用小节 1 和小节 2它们,但这意味着必须有人实际为它们分配空间。

    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;
        }
    };

还有一个主要的 CPP 来演示使用

#include <iostream>
#include "space.h"

// allocate the bars and the 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;
}

【讨论】:

  • 昨晚我终于发现了外部镜头。但是另外两个镜头很吸引人。由于完整性和清晰性,答案将交给您。
猜你喜欢
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 2011-01-11
  • 2011-05-25
  • 2012-10-13
  • 1970-01-01
相关资源
最近更新 更多