Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

explicit c++

#include <iostream>
#include <cstring>

Class MyClass{
public:
  explicit MyClass(const char* str){
    std::cout << str << std::endl;
  }
};

int main(int argc, char* argv[]){
  std::string title = "Hello World!";
  MyClass obj1(title); // This will not compile, because with explicit
  //                      keyword we blocked implicit convertion from
  //                      std::string to const char*
  MyClass obj2(title.c_str()) // Now it works, because we paste in a
  //                      const char* with c_str() method!
  
  return 0;
}
 
PREVIOUS NEXT
Tagged: #explicit
ADD COMMENT
Topic
Name
1+6 =