重载 ostream <<静态类的运算符?

发布于 2024-08-11 06:40:16 字数 483 浏览 4 评论 0 原文

我有一个(简化的)静态全局类和 <<运算符重载如下:

class Global
{
    private:
    static int counter;
    Global(){};

    public:
    friend ostream& operator<<(ostream &out, Global &global);
}

ostream& operator<< (ostream &out, Global &global)
{
    //... do output
    return out;
}

我希望能够将静态引用传递给 cout:

cout << Global

但是,<<运算符想要一个实例,但实际上不存在该全局类的实例。这附近还有吗?

感谢您的任何帮助。

I have a (simplified) static global class and << operator overload as follows:

class Global
{
    private:
    static int counter;
    Global(){};

    public:
    friend ostream& operator<<(ostream &out, Global &global);
}

ostream& operator<< (ostream &out, Global &global)
{
    //... do output
    return out;
}

I want to be able to pass a static reference to cout:

cout << Global

However, the << operator wants an instance, yet no instances of this global class actually exist. Is there anyway around this?

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

子栖 2024-08-18 06:40:16

首先,你不能使用类名作为值——它根本就不是一个值。因此,您必须引入一个与 << 一起使用的不同名称 - 例如 global (使用小写“g”)。

一般来说,如果您想在不定义对象的情况下引入“可流式”名称,则应该编写一个流操纵器:

std::ostream& foo(std::ostream& out)
{
    out << "foo";
    return out;
}

这里的技巧是流具有重载运算符 << 这样,如果您将函数指针传递给它,并且该函数接受并返回一个流,那么 << 将相当于将该函数应用于该流。换句话说,您可以编写:

std::cout << 123 << foo << 456;

它将与以下内容相同:

foo(std::cout << 123) << 456;

例如,这就是 std::endl 的实现方式。

同样的事情也适用于 >>,如果您愿意,您可以在 basic_istream 和/或 basic_ostream 上提供模板函数更通用。

First of all, you cannot use a class name as a value - it simply isn't one. So you'll have to introduce a different name for use with << - say, global (with lowercase "g").

In general, if you want to introduce a "streamable" name without defining an object, you should write a stream manipulator:

std::ostream& foo(std::ostream& out)
{
    out << "foo";
    return out;
}

The trick here is that streams have overloaded operator << such that, if you pass a function pointer to it, and that function takes and returns a stream, then << will be equivalent to applying the function to the stream. In other words, you are able to write:

std::cout << 123 << foo << 456;

and it'll be the same as:

foo(std::cout << 123) << 456;

It's how std::endl is implemented, for example.

The same thing also applies to >>, and you can provide template function on basic_istream and/or basic_ostream if you want it to be more generic.

自控 2024-08-18 06:40:16

单例模式

这是您可以实现给定示例的模式的一种方法(不检查错误):

class Global
{
   private:
   static int counter;
   Global(){};
   static Global *_instance;

   public:
   static Global getInstance() { 
     if (!_instance) 
        _instance = new Global();
     return *_instance; 
   }
   friend ostream& operator<<(ostream &out, Global &global);
}

Global* Global::_instance = NULL;

ostream& operator<< (ostream &out, Global &global)
{
    //... do output
    return out;
}

然后您的调用代码将如下所示:

cout << Global::getInstance()

Singleton pattern.

Here's one way you might implement the pattern given your example (not error checked):

class Global
{
   private:
   static int counter;
   Global(){};
   static Global *_instance;

   public:
   static Global getInstance() { 
     if (!_instance) 
        _instance = new Global();
     return *_instance; 
   }
   friend ostream& operator<<(ostream &out, Global &global);
}

Global* Global::_instance = NULL;

ostream& operator<< (ostream &out, Global &global)
{
    //... do output
    return out;
}

Then your calling code would look like:

cout << Global::getInstance()
記憶穿過時間隧道 2024-08-18 06:40:16

如果您真的想要在没有对象实例的情况下调用这样的函数,您可以这样做:

std::cout << *(Global*)NULL;

但是已经建议的单例模式是一个更好的主意。

If you really wanted to call such a function with no object instance you could do it like this:

std::cout << *(Global*)NULL;

But the singleton pattern already suggested is a much better idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文