简体   繁体   English

“状态”在 C++ 中是什么意思?

[英]What does "state" mean in C++?

When I was looking for explanations of C++ functor, I saw the following statement, "There are a couple of nice things about functors. One is that unlike regular functions, they can contain state."当我在寻找对 C++ 函子的解释时,我看到了以下陈述,“函子有一些不错的地方。一个是与常规函数不同的是,它们可以包含状态。”

Could anybody explain to me what "state" means in C++?有人能向我解释一下 C++ 中的“状态”是什么意思吗? Thank you very much.非常感谢。

What does “state” mean ... “状态”是什么意思...

The word has multiple meanings and contextual subtleties.这个词有多重含义和上下文微妙之处。

Here is a general definition for the word from a dictionary :这是字典中单词的一般定义:

a condition or way of being that exists at a particular time在特定时间存在的条件或存在方式


... in C++? ...在 C++ 中?

There is no C++ specific meaning for the word as far as I know.据我所知,这个词没有 C++ 特定的含义。 It is not something specified by the language.它不是由语言指定的东西。 The meaning is same as in programming or computer science in general.其含义与一般的编程或计算机科学相同。

Here is a computer science specific definition :这是计算机科学的特定定义

In information technology and computer science, a system is described as stateful if it is designed to remember preceding events or user interactions;在信息技术和计算机科学中,如果系统旨在记住先前的事件或用户交互,则系统被描述为有状态的; the remembered information is called the state of the system.记住的信息称为系统状态。

The state of a C++ program consists primarily of the representation of objects. C++ 程序的状态主要由对象的表示组成。


"There are a couple of nice things about functors. One is that unlike regular functions, they can contain state." “函子有一些不错的地方。一是与常规函数不同,它们可以包含状态。”

While this is "true enough" in practice, it is a simplification.虽然这在实践中“足够真实”,但它是一种简化。 Technically regular functions can "contain" global state.从技术上讲,常规函数可以“包含”全局状态。 But that is probably ignored by the author of that quote since global state is problematic and something that should be avoided.但这句话的作者可能忽略了这一点,因为全局状态是有问题的,应该避免。

Assuming you are actually asking what it means in this context :假设您实际上是问它在这种情况下的含义:

It means that a functor (ie an instance of a class implementing the () operator) can store and access information that is related to its particular instance.这意味着函子(即实现()运算符的类的实例)可以存储和访问与其特定实例相关的信息。

A regular function can only access whatever parameters are passed into it, plus global variables etc.常规函数只能访问传递给它的任何参数,以及全局变量等。

Example with a functor however:但是,带有函子的示例:

#include <iostream>

struct Counter {
    int operator()() { return ++count; }

  private:
    int count = 0;
};

Counter count1;
Counter count2;

std::cout << count1() << std::endl; // 1
std::cout << count1() << std::endl; // 2
std::cout << count1() << std::endl; // 3

std::cout << count2() << std::endl; // 1
std::cout << count2() << std::endl; // 2
std::cout << count2() << std::endl; // 3

std::cout << count1() << std::endl; // 4
std::cout << count2() << std::endl; // 4

Here, the actual count is encapsulated in the functor instance - it is the functor's state .在这里,实际计数被封装在函子实例中——它是函子的状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM