explicit String(const char* chars) {
}
String(const char* s0, const char* s1) {
cout << s0 << endl;
cout << s1 << endl;
}
int main(){
// String(s) = "abc"; 암시적(묵시적) 호출이기 때문에 explicit에 의하여 컴파일 에러
String s0("abc");
String s1 = String("abc" , "def");
}
abc
def
String(std::initializer_list<const char*> strs) {
for (const char* str : strs) {
cout << str << endl;
}
}
int main(){
String s2{ "A1", "B1", "C1", "D1" };
}
A1
B1
C1
D1
#pragma warning(disable : 4996)
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char* _chars;
public:
String() = default;
String(const char* chars)
: _chars(new char[strlen(chars) + 1]) {
strcpy(_chars, chars);
}
explicit operator bool() const {
return strlen(_chars) > 0;
}
};
int main() {
String s0 = "abc";
bool result = (bool)s0; // explicit로 인해 강제 형변환을 시킴
cout << result << endl;
}
1