zoukankan      html  css  js  c++  java
  • 运算符重载方法2

    C++允许自己编辑自己版本的加号,以正确处理类,

    #include <iostream>
    //#include <string>
    #include <sstream>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    class A
    {
        public:
            A();
            A(int initialValue);
            A(std::string & initialValue);
            A add(const A & src);
            A operator +(const A & rhs);
            void setValue(int value);
            void setString(std::string& string);
            void showValue();
            void showString();
        private:
            int mValue;
            std::string mString;
    };
    
    A::A()
    :mValue(0)
    {
        
    }
    A::A(int initialValue)
    :mValue(initialValue)
    {
        
    }
    
    A::A(std::string & initialValue)
    :mString(initialValue)
    {
        
    }
    void A::setValue(int value)
    {
        mValue = value;
    }
    void A::setString(std::string & string)
    {
        std::stringstream ss;
        ss << mString;
        ss << string;
        mString = ss.str();
    }
    A A::add(const A & src)
    {
        A newA;
        newA.setValue(mValue + src.mValue);
        return newA;
    }
    
    A A::operator +(const A & rhs)
    {
        A newA;
        newA.setValue(mValue + rhs.mValue);
        newA.setString(mString.append(rhs.mString));
        return newA;
    }
    void A::showValue()
    {
        std::cout << "value : " << mValue << std::endl;
    }
    void A::showString()
    {
        std::cout << "string : " << mString << std::endl;
    }
    
    int main(int argc, char** argv) 
    {
        A a1(5);
        A a2(6);
        std::string s1 = "AAAA";
        std::string s2 = "BBBB";
        a1.setString(s1);
        a2.setString(s2);
        A a3 = a1 + a2;
        a3.showValue();
        a3.showString();
        return 0;
    }

    运算结果:

    value : 11
    string : AAAABBBB

    在operator和+之间可以有空格也可以没有空格,operator+是一个整体就像是函数名f00,add一样,如果在类A中编写了operator+的方法,那么

    A a3 = a1 + a2;这一行就会转化为A a3 = a1 operator + a2;

    在main函数体内这样编写:

    int main(int argc, char** argv) 
    {
        A a1(1),a2;
        std::string str = "AAA";
        a2 = a1 + str;
        a2.showValue();
        a2.showString();
        return 0;
    }

    结果是:

    value : 49
    string : AAA

    这是我们想不到的结果,但是细细看来发现,这是隐式转换作的怪,上述代码之所以能够运行是因为编译器会试着查找合适的operator+而并不是之查找定义类型的operator+

    ,为了找到合适的operator+,编译器会查找合适的类型转换,构造函数会对有问题的类型进行适当的转换,当A实列试图与int相加,发现有用int作为参数的构造函数就会构造临时的A对象,传递给operator+,与此类似,当碰见std::string类型相加的时候,会找到以std::string类型为参数的构造函数来创建一个临时的A对象来传递给operator+.

    为了防止这样的情况发生我们可以阻止这样的隐式转换,关键字explicit.

    explicit A(std::string & initialValue);

    这样在实现a2 = a1 + str;编译器就会报错

    explicit的作用就是避免编译器自动隐式转换。

     
  • 相关阅读:
    Linux中Postfix邮件安装Maildrop(八)
    Linux中Postfix邮件WebMail配置(七)
    Linux中Postfix虚拟用户及虚拟域(六)
    Linux中Postfix邮件认证配置(五)
    Linux中Postfix邮件接收配置(四)
    Go构建工程入门1(了解即可,现在推荐使用Go module)
    Pod 启动流程详解
    Go Modules详解
    k8s 学习资源备忘录
    Kubernetes 架构及核心概念
  • 原文地址:https://www.cnblogs.com/boost/p/10334948.html
Copyright © 2011-2022 走看看