这可能是一个奇怪的问题,但我正在尝试找到一种方法来破坏该gmtime_r功能。我正在为这段代码编写一个测试,但我找不到一个案例来到达不包含设置ptr或datesto的 else 语句NULL。
int main() {
time_t ptr; // Actual storage for time_t
struct tm dates; // Actual storage for struct tm
time(&ptr); // Pointer to a time_t
if(gmtime_r(&ptr, &dates)) { // Pointer to a time_t, pointer to a
size_t a = 10;
}
else
{
size_t a = 20; //<-- trying to reach this else statement
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何有效的非 NULL 值可以设置ptr,或者dates可以绕过 if 语句并进入 else ?
我正在尝试通过阅读教科书并做练习题和其他东西来自学 C++,我目前正在学习的主题对我来说有点混乱,我希望得到一些澄清。我在网上寻找我的问题的明确答案,但还没有找到任何东西。
我目前正在学习标准库中 IO 类的详细信息,我现在所在的部分给出了一些具有传递和返回 IO 对象的函数的示例。
例如:
istream &get_value(istream &input)
{
int value;
input >> value;
return input;
}
int main()
{
get_value(cin);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从高层次的角度理解这里发生的事情。该get_value函数具有对输入对象类型的引用,并且还接受对输入对象的引用,在我的示例中,我使用了常用cin对象。我知道此函数正在控制台中读取用户的输入并将该输入存储为value.
我不明白的是返回输入对象的原因是什么。为什么这个函数不应该有一个类型void?我正在使用的输入对象可以用来做什么?我知道我现在没有用它做任何事情,但它可以用来做什么?
我想知道是否可以将枚举类初始化为 null。我写了一个简短的例子来说明我要问的问题。
我这里有一个标头定义了一个名为的枚举类ColorOptions
#ifndef COLORS_HPP
#define COLORS_HPP
enum class ColorOptions
{
RED,
BLUE
};
#endif
Run Code Online (Sandbox Code Playgroud)
我还有一个类使用此枚举类根据枚举值打印颜色
#include "Colors.hpp"
#include <iostream>
void printColor(ColorOptions col);
int main()
{
printColor(ColorOptions::RED);
printColor(ColorOptions::BLUE);
}
void printColor(ColorOptions col)
{
switch(col)
{
case ColorOptions::RED:
std::cout << "The color is red" << std::endl;
break;
case ColorOptions::BLUE:
std::cout << "The color is blue" << std::endl;
break;
default:
std::cout << "The color is unknown" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,是否可以将 a 初始化ColorOptions为REDor以外的东西BLUE?我想达到该printColor方法的默认情况,但我不确定是否可以在不向 …