函数不显示返回值 - C++
Function not displaying return value - C++
经过一番修补,我终于想出了一个函数来确定任何值的数据类型(在固定范围内(。
但是当我尝试打印出此函数的返回值(char*
(时,什么都没有打印出来。
template <typename data> inline char* typeOf(data arg) {
const std::type_info& type = typeid(arg);
char* typeName;
if (type == typeid(bool)) strcpy(typeName, "boolean");
else if (
type == typeid(double) ||
type == typeid(float) ||
type == typeid(int) ||
type == typeid(long double) || type == typeid(long int) || type == typeid(long long) ||
type == typeid(signed int) || type == typeid(signed long int) || type == typeid(signed short int) ||
type == typeid(short int) ||
type == typeid(unsigned int) || type == typeid(unsigned long int) || type == typeid(unsigned short int)
) strcpy(typeName, "number");
else if (
type == typeid(char) ||
type == typeid(signed char) ||
type == typeid(std::string) ||
type == typeid(unsigned char) ||
type == typeid(wchar_t)
) strcpy(typeName, "string");
else if (type == typeid(void)) strcpy(typeName, "void");
else strcpy(typeName, "null");
// Expectation: Print out the value here
// Problem: It does not print anything!
std::cout << typeName << std::endl;
return typeName;
}
我敢肯定这是我不熟悉的东西,但任何帮助解释为什么没有从std::cout
打印任何东西将不胜感激。目标是让函数确定值的数据类型,并基于该数据类型返回字符串。
对此功能的任何改进也将不胜感激。
将char* typeName
更改为char* typeName=new char[50]
例如,50只是一个数字,是一个可以包含您在代码中编写的所有字符串的维度。请注意,strcpy(char* destination, const char* source( 要求目标指向大小大于源的位置,以便目标可以包含所有源。我建议你使用类型名称的字符串而不是字符*。请记住,返回的指向 char* 的指针需要在代码中的某处删除。
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 对fread的返回值感到困惑
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 使用 std::p air 进行返回值优化
- C++输出参数与返回值