gpt4 book ai didi

javascript - 像 Javascript 一样在 C++ 中访问 HttpStatusCode

转载 作者:行者123 更新时间:2023-11-28 01:31:37 27 4
gpt4 key购买 nike

我正在 ESP 上编写一个 Arduino 项目。

在 Javascript(客户端)中我有这个:

HttpStatusCode = { // Top10
OK: 200,
Created: 201,
NoContent: 204,
NotModified: 304,
BadRequest: 400,
Unauthorized: 401,
Forbidden: 403,
NotFound: 404,
Conflict: 409,
InternalServerError: 500
};

在我的代码中

   if( this.status == HttpStatusCode.OK ) {
...
}

现在,在 C++(服务器端)中,您如何做同样的事情?

   if( status == HttpStatusCode.OK ) {
...
}

最佳答案

在 C++11 及更高版本中,您可以使用 scoped enum ,例如:

enum class HttpStatusCode { // Top10
OK = 200,
Created = 201,
NoContent = 204,
NotModified = 304,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
Conflict = 409,
InternalServerError = 500
};

否则,使用一个classstruct,其中包含static常量,例如:

struct HttpStatusCode {
static const int OK = 200;
static const int Created = 201;
static const int NoContent = 204;
static const int NotModified = 304;
static const int BadRequest = 400;
static const int Unauthorized = 401;
static const int Forbidden = 403;
static const int NotFound = 404;
static const int Conflict = 409;
static const int InternalServerError = 500;
};

无论哪种方式,您都可以像这样使用它们:

if( status == HttpStatusCode::OK )

关于javascript - 像 Javascript 一样在 C++ 中访问 HttpStatusCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275675/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com