Explicit Sepcifier

本文探讨了显式指定符(Explicit Specifier)在编程中的作用,特别是在转换构造函数中的影响。首先,解释了转换构造函数如何允许隐式类型转换,例如将两个double数值转化为复数对象,或者单个double数值进行转化。接着,指出通过在构造函数前添加显式指定符,可以阻止这种隐式转换,从而避免潜在的错误。最后,提到了显式类型转换(Explicitly Typecast)的使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Conversion Constructor

#include <iostream> 

using namespace std; 

class Complex 
{ 
private: 
	double real; 
	double imag; 

public: 
	// no default arguments -> a conversion constructor
	Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} 

	// A method to compare two Complex numbers 
	bool operator == (Complex rhs) { 
	return (real == rhs.real && imag == rhs.imag)? true : false; 
	} 
}; 

int main() 
{ 
	// a Complex object 
	Complex com1 = {3.0, 0.0}; 

	if (com1 == 3.0) 
	cout << "Same"; 
	else
	cout << "Not Same"; 
	return 0; 
} 

Output: Same
Explanation: There are two implicit conversions happened in this code snippet.

  1. Complex com1 = {3.0, 0.0}; Use 2 double numbers to construct a complex object.
  2. if (com1 == 3.0) Use 1 double number to construct a complex object.

2. Explicit Specifier

If we add explicit before Constructor, we then will get an error.

class Complex 
{ 
private: 
	double real; 
	double imag; 

public: 
	// no default arguments -> a conversion constructor
	explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} 

	// A method to compare two Complex numbers 
	bool operator == (Complex rhs) { 
	return (real == rhs.real && imag == rhs.imag)? true : false; 
	} 
}; 

Output: error: chosen constructor is explicit in copy-initialization.

3. Explicitly Typecast

int main() 
{ 
	// a Complex object 
	Complex com1(3.0, 0.0); 

	if (com1 == static_cast<Complex>(3.0)) 
	cout << "Same"; 
	else
	cout << "Not Same"; 
	return 0; 
} 

Output: Same

`explicit` 是 C++ 中的一个关键字,主要用于修饰构造函数,其目的是防止隐式类型转换和拷贝初始化带来的意外错误。 ### 默认情况下的隐式转换 在 C++ 中,单参数的构造函数默认可以用于类型之间的隐式转换。这种特性有时很方便,但也可能导致一些难以察觉的问题。 #### 示例:未使用 `explicit` ```cpp class MyClass { public: MyClass(int value) : m_value(value) {} void print() const { std::cout << "Value: " << m_value << '\n'; } private: int m_value; }; void test(MyClass obj) { obj.print(); } int main() { test(42); // 隐式将整数 42 转换为 MyClass 对象 } ``` 在这个例子中,当你调用 `test(42)` 的时候,编译器自动把 `42` 这个整型值通过 `MyClass(int)` 构造函数转化为一个临时的 `MyClass` 实例,并将其传递给 `test()` 函数。 尽管这看起来没什么问题,但在某些场景下可能会导致意想不到的行为。 --- ### 使用 `explicit` 禁止隐式转换 如果我们希望避免这种情况的发生,就可以使用 `explicit` 关键字声明单参构造函数: ```cpp class MyClass { public: explicit MyClass(int value) : m_value(value) {} // 加上 explicit void print() const { std::cout << "Value: " << m_value << '\n'; } private: int m_value; }; ``` 此时如果尝试运行前面同样的代码,则会发生编译期错误: ```cpp error: no matching function for call to 'test' note: candidate function not viable: expects an argument of type 'MyClass', but found 'int'. ``` 只有显式地创建对象才能正常工作: ```cpp MyClass mc = MyClass{42}; // OK - 显式转换 test(mc); ``` 或者直接传入已实例化的对象而非原始数据类型: ```cpp test(MyClass{42}); // 此处由于是显示指定构建过程所以依然有效。 ``` 总结来说,当您不想让某个特定类型的单一参数构造函数被用来做隐式转型的时候就应当考虑应用这个关键词以增强代码安全性与清晰度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值