automatictypeconversion.cpp

来自「ThinkingC++中文版」· C++ 代码 · 共 64 行

CPP
64
字号
//: C12:AutomaticTypeConversion.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Type conversion constructor


//构造函数类型转换
class One {
public:	
	One() {}
};

class Two {
public:
	Two(){}
    Two(const One&) {}  //具有一个参数的构造函数称为:automatic type conversion,
};

void f(Two) {}

void main() {
  One one;
  f(one); // Wants a Two, has a One
/*When the compiler sees f( ) called with a One object, it looks at 
  the declaration for f( ) and notices it wants a Two.
  Then it looks to see if there’s any way to get a Two from a One, 
  and it finds the constructor Two::Two(One),which it quietly calls.
  The resulting Two object is handed to f( ).
*/
 // f(Two(one)); 
} 
//通过构造函数自动类型转换,避免了定义两个f()重载版本的麻烦,然而代价是隐藏了构造函数的调用 .
//如果我们刻意追求f()函数的调用效率,就应该避免。


/*
//阻止构造函数隐含类型转换
class One {
public:	
	One() {}
};

class Two {
public:
	Two(){}
    explicit Two(const One&) {} 
    //Preventing constructor conversion:There are times when automatic type conversion 
    //via the constructor can cause problems. To turn it off, 
};

void f(Two) {}

void main() {
  One one;
  //f(one); //error,no auto conversion allowed.
  f(Two(one)); 
}
*/



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?