zoukankan      html  css  js  c++  java
  • 隐式的类类型转换

    如果构造函数只接受一个实参,则它实际上定义了转换为此类类型的隐式转换机制。将这种构造函数称为转换构造函数。

    1. #ifndef MAIN_H_INCLUDED
    2. #define MAIN_H_INCLUDED
    3. #include<iostream>
    4. usingnamespace std;
    5. classClassTest
    6. {
    7. public:
    8. ClassTest()
    9. {
    10. cout <<"ClassTest()"<<endl;
    11. }
    12. ClassTest(int i)
    13. {
    14. cout <<"ClassTest(int)"<<endl;
    15. value = i;
    16. value2 =2;
    17. }
    18. ClassTest(int i,int j): value(i), value2(j)
    19. {
    20. cout <<"ClassTest(int, int)"<<endl;
    21. }
    22. ClassTest(constClassTest& test)
    23. {
    24. cout <<"ClassTest(const ClassTest&)"<< endl;
    25. value = test.value;
    26. value2 = test.value2;
    27. }
    28. void getValue(int&,int&);
    29. private:
    30. int value =0;
    31. int value2 =0;
    32. };
    33. #endif// MAIN_H_INCLUDED
    1. #include"main.h"
    2. voidClassTest::getValue(int&iValue,int&iValue2)
    3. {
    4. iValue = value;
    5. iValue2 = value2;
    6. }
    7. int main()
    8. {
    9. ClassTest classTest =2;
    10. /**
    11. * 还可以使用以下几种实现方法
    12. * ClassTest classTest(2);
    13. * ClassTest classTest = ClassTest(2);
    14. * ClassTest classTest = (ClassTest)2;
    15. */
    16. int value =0;
    17. int value2 =0;
    18. classTest.getValue(value, value2);
    19. cout <<"value = "<< value <<" value2 = "<< value2 << endl;
    20. return0;
    21. }
    • 只允许一步类类型转换
    1. #ifndef MAIN_H_INCLUDED
    2. #define MAIN_H_INCLUDED
    3. #include<iostream>
    4. usingnamespace std;
    5. classClassTest
    6. {
    7. public:
    8. ClassTest()
    9. {
    10. cout <<"ClassTest()"<<endl;
    11. }
    12. ClassTest(string str)
    13. {
    14. strValue = str;
    15. }
    16. string getValue()
    17. {
    18. return strValue;
    19. }
    20. private:
    21. string strValue;
    22. };
    23. #endif// MAIN_H_INCLUDED
    1. #include"main.h"
    2. int main()
    3. {
    4. ClassTest classTest = string("Hello world!");
    5. //ClassTest classTest = "Hello world!";/*无法实现从char[]先转化为string,在由string转换为类类型*/
    6. cout << classTest.getValue()<< endl;
    7. return0;
    8. }
    • 抑制构造函数定义的隐式转换
    1. #ifndef MAIN_H_INCLUDED
    2. #define MAIN_H_INCLUDED
    3. #include<iostream>
    4. usingnamespace std;
    5. classClassTest
    6. {
    7. public:
    8. ClassTest()
    9. {
    10. cout <<"ClassTest()"<< endl;
    11. }
    12. /**
    13. * 如果是在类声明之外进行类成员函数的实现,explicit只加在声明的位置
    14. */
    15. ClassTest(string str)
    16. {
    17. strValue = str;
    18. }
    19. string getValue()
    20. {
    21. return strValue;
    22. }
    23. private:
    24. string strValue;
    25. };
    26. #endif// MAIN_H_INCLUDED
    1. #include"main.h"
    2. int main()
    3. {
    4. //ClassTest classTest = string("Hello world!");/**< 错误 */
    5. ClassTest classTest(string("hello world"));/**< 只能用于直接初始化 */
    6. //ClassTest classTest = "Hello world!";/*无法实现从char[]先转化为string,在由string转换为类类型*/
    7. cout << classTest.getValue()<< endl;
    8. return0;
    9. }
     





  • 相关阅读:
    C++常用工具收集
    Ubuntu禁用触摸板
    Vim简本
    JS原型链模式和继承模式
    JS原型链模式
    JS中的单例模式/工厂模式/构造函数模式(并非完全意义上的设计模式)
    JS中一道关于this和闭包的题
    JS中的this关键字
    JS闭包
    JS作用域和作用域链
  • 原文地址:https://www.cnblogs.com/fengkang1008/p/4652252.html
Copyright © 2011-2022 走看看