1、explicit关键字介绍
C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 关键字是implicit, 意思是隐藏的,类构造函数默认情况下即声明为implicit(隐式).
2、贴代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Stest
{
public:
char *_pstr;
int _size;
// explicit Stest(int size)
Stest(int size)
{
_size = size;
_pstr =(char *)malloc(size + 1);
memset(_pstr, 0, size + 1);
}
Stest(const char* p)
{
int size = strlen(p);
cout<<"size"<<size<<endl;
_pstr = (char *)malloc(size + 1);
strcpy(_pstr, p);
_size = strlen(_pstr);
cout<<"_size"<<size<<endl;
}
};
int main()
{
Stest s;
Stest s1(30);
Stest s2