kenics.net

Technical notes on perl, python, php, sql, cgi, c/c++, q/kdb+, unix/shell, revision control tools, data structures & algorithms, and their applications into web services and other various forms of software engineering.

C++ explicit specifier

 
#################################### 
####    "explicit"  specifier    ### 
#################################### 
 
as discussed in the initialization section of the class doc, when you have the below example class, CParent, the following four instantiations are all correct. 
 
------------------------------------ 
 
class CParent{ 
   public: 
     CParent(); 
     CParent(int); 
}; 
 
CParent cp;                 // calls CParent::CParent(); 
CParent cp(123);            // calls CParent::CParent(int); 
CParent cp = 123;           // same as above 
CParent cp = CParent(123);  // same as above 
 
cp = 123;                   // this works because 123 is implicitly converted to a temp obj initialized from CParent::CParent(int), then copies to cp instance via the assignment operator 
 
------------------------------------ 
 
lets get the above code to a more realistic example. 
 
------------------------------------ 
 
#include <iostream> 
using namespace std; 
 
class CParent{ 
  public: 
     CParent(): px(777){ 
       cout << "constructor ONE" << endl; 
     } 
     CParent(int x){ 
  cout << "constructor TWO" << endl; 
       px = x; 

     int show(){return px;} 
  private: 
     int px; 
}; 
 
int main() 

  CParent cp0;                  // constructor ONE 
  cp0 = 123;                    // constructor TWO  // implicit conversion 
  cout << cp0.show() << endl;   // 123 
 
  CParent cp1(123);             // constructor TWO 
  CParent cp2 = 123;            // constructor TWO  // implicit conversion 
  CParent cp3 = CParent(123);   // constructor TWO 
  CParent cp4 = (CParent)123;   // constructor TWO  // explicit casting 
 
  return 0; 

 
------------------------------------- 
 
Now we add "explicit" specifier to CParent::CParent(int) definition to prevent implicit type conversion. 
 
 
------------------------------------- 
 
#include <iostream> 
using namespace std; 
 
class CParent{ 
  public: 
     CParent(): px(777){ 
       cout << "constructor ONE" << endl; 
     } 
     explicit CParent(int x){                     // added "explicit" 
       cout << "constructor TWO" << endl; 
       px = x; 
     } 
     int show(){return px;} 
  private: 
     int px; 
}; 
 
int main() 

  CParent cp0;                  // constructor ONE 
  cp0 = 123;                    // error 
  cout << cp0.show() << endl;   // 123 
 
  CParent cp1(123);             // constructor TWO 
  CParent cp2 = 123;            // error 
  CParent cp3 = CParent(123);   // constructor TWO 
  CParent cp4 = (CParent)123;   // constructor TWO  // explicit casting 
 
  return 0; 

 
--------------------------------------- 
 
 
lastly, an intersting example below with 
(1) explicit constructor CParent::CParent(int) 
(2) non-explicit constrcutor CParent::CParent(bool) 
 
---------------------------------------- 
 
#include <iostream> 
using namespace std; 
 
class CParent{ 
  public: 
     CParent(): px(777){ 
       cout << "constructor ONE" << endl; 
     } 
     explicit CParent(int x){                     // explicit 
       cout << "constructor TWO" << endl; 
       px = x; 
     } 
CParent(bool y){                             // non-explicit 
       cout << "constructor THREE" << endl; 
     } 
     int show(){return px;} 
  private: 
     int px; 
}; 
 
int main() 

  CParent cp0;                  // constructor ONE 
  cp0 = 123;                    // constructor THREE  // implicit type conversion (123 -> bool) 
  cout << cp0.show() << endl;   // 123 
 
  CParent cp1(123);             // constructor TWO 
  CParent cp2 = 123;            // constructor THREE  // implicit type conversion (123 -> bool) 
  CParent cp3 = CParent(123);   // constructor TWO 
  CParent cp4 = (CParent)123;   // constructor TWO  // explicit casting 
 
  return 0; 

 
------------------------------------------ 
 
(ref) 
http://www.glenmccl.com/tip_023.htm 
http://www.parashift.com/c++-faq/explicit-ctors.html 
 
 
 
 

  1. 2014-05-30 21:41:36 |
  2. Category : cpp
  3. Page View:

Google Ads