#include<string>
class HelloWorld{
private:
std::string outString;
public:
/*
HelloWorld (){
outString = "jilichuan";
}
*/
void output() const{
if(this->outString == ""){
std::cout<<"outString is NULL";
}
else{
std::cout<<this->outString;
}
}
};
class HelloWorld{
private:
std::string outString;
public:
/*
HelloWorld (){
outString = "jilichuan";
}
*/
void output() const{
if(this->outString == ""){
std::cout<<"outString is NULL";
}
else{
std::cout<<this->outString;
}
}
};
#include <iostream>
#include "helloworld.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
HelloWorld hw;
hw.output();
return 0;
#include "helloworld.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
HelloWorld hw;
hw.output();
return 0;
}
发现,没有对string进行初始化,就已经默认可以使用,并且是空串,一直用java,对c++不熟悉。搜索了下,发现在c++中,只要对对象进行了定义,如果没有初始化,就会自动调用默认构造函数。