Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++;(类似于std::从const char*初始化的字符串) 我一直在想,在许多不同的语言中,现在C++中,你是如何用一个本原初始化一个对象的?我很好奇std::string是如何用const char*初始化的,我想知道是否有一种简单的方法可以做到这一点。抱歉,如果这个问题的格式不正确,我只是在stackoverflow上没有发现类似的问题,这是我的第一个或第二个问题。谢谢_C++_Oop - Fatal编程技术网

如何在C++;(类似于std::从const char*初始化的字符串) 我一直在想,在许多不同的语言中,现在C++中,你是如何用一个本原初始化一个对象的?我很好奇std::string是如何用const char*初始化的,我想知道是否有一种简单的方法可以做到这一点。抱歉,如果这个问题的格式不正确,我只是在stackoverflow上没有发现类似的问题,这是我的第一个或第二个问题。谢谢

如何在C++;(类似于std::从const char*初始化的字符串) 我一直在想,在许多不同的语言中,现在C++中,你是如何用一个本原初始化一个对象的?我很好奇std::string是如何用const char*初始化的,我想知道是否有一种简单的方法可以做到这一点。抱歉,如果这个问题的格式不正确,我只是在stackoverflow上没有发现类似的问题,这是我的第一个或第二个问题。谢谢,c++,oop,C++,Oop,如果你可以这样写: std::string s=“你好,世界!”; 这是因为类的构造函数采用const char* std::string是一个std::basic\u字符串 例如,如果您想用基元数据类型实例化自定义类的对象,只需定义一个构造函数即可: 类MyCustomClass { 公众: MyCustomClass(int a):m_整数(a){} MyCustomClass(const char*s):m_整数(std::atoi(s)){} int getValue()常量{retu

如果你可以这样写:

std::string s=“你好,世界!”;
这是因为类的构造函数采用
const char*

std::string
是一个
std::basic\u字符串

例如,如果您想用基元数据类型实例化自定义类的对象,只需定义一个构造函数即可:

类MyCustomClass
{
公众:
MyCustomClass(int a):m_整数(a){}
MyCustomClass(const char*s):m_整数(std::atoi(s)){}
int getValue()常量{return m_integer;}
私人:
整数;
};
此类允许您像这样使用它:

MyCustomClass mcc1=2;//使用MyCustomClass(int a)
MyCustomClass mcc2=“12”//使用MyCustomClass(常量字符*s)
//将打印:2

std::cout如果你可以这样写:

std::string s=“你好,世界!”;
这是因为类的构造函数采用
const char*

std::string
是一个
std::basic\u字符串

例如,如果您想用基元数据类型实例化自定义类的对象,只需定义一个构造函数即可:

类MyCustomClass
{
公众:
MyCustomClass(int a):m_整数(a){}
MyCustomClass(const char*s):m_整数(std::atoi(s)){}
int getValue()常量{return m_integer;}
私人:
整数;
};
此类允许您像这样使用它:

MyCustomClass mcc1=2;//使用MyCustomClass(int a)
MyCustomClass mcc2=“12”//使用MyCustomClass(常量字符*s)
//将打印:2

std::难道它只是有一个构造函数,它接受一个
常量char*
。哦,真的吗?我不知道用C++工作,谢谢!它只是有一个构造函数,它接受一个
常量char*
。哦,真的吗?我不知道用C++工作,谢谢!从技术上讲,
std::string
的工作要比这复杂得多。事实上,
std::string
实际上并没有定义为类-它是模板化类专门化的类型别名。该模板类的构造函数在构造时会导致
std::string
接受
const char*
,它接受两个参数。@Peter,是的,非常精确,但为了简化理解,我编写了这个示例。我添加了一些小的精确性。@thibsc-别担心,我理解。原则上,你的答案是正确的,我也相应地投了赞成票。只是,对于
std::string
的特定情况,您所说的与
std::string
的用户相关,但是实现比您所描述的要复杂一些(出于各种原因)。从技术上讲,
std::string
的工作要复杂得多。事实上,
std::string
实际上并没有定义为类-它是模板化类专门化的类型别名。该模板类的构造函数在构造时会导致
std::string
接受
const char*
,它接受两个参数。@Peter,是的,非常精确,但为了简化理解,我编写了这个示例。我添加了一些小的精确性。@thibsc-别担心,我理解。原则上,你的答案是正确的,我也相应地投了赞成票。只是,对于
std::string
的特定情况,您所说的与
std::string
的用户相关,但是实现比您描述的要复杂一些(出于各种原因)。