| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3793 人关注过本帖
标题:关于转换构造函数隐示调用的问题!
只看楼主 加入收藏
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
结帖率:90.24%
收藏
已结贴  问题点数:20 回复次数:5 
关于转换构造函数隐示调用的问题!
//程序段一:
程序代码:
#include <iostream>

using namespace std;

class Complex
{
    public:
        Complex(int r,int i):real(r),imag(i){}
        Complex operator ++();                                 //    相当于 ++Time ,单操作数运算符的重载 
        Complex operator ++(int);                            //  相当于 Time++ 
        Complex (int r);                                    //    此乃转换构造函数 
//        Complex operator +(Complex &tmp);                    //    使用类的成员函数进行“+”运算符的重载 
        friend Complex operator +(Complex &tmp1,Complex &tmp2);    
        friend ostream& operator<<(ostream& output,Complex& tmp);
        friend istream& operator>>(istream& input,Complex& tmp);
        

    private:
        int real;
        int imag;
};
/*
Complex Complex::operator +(Complex &tmp)
{
    return Complex(this->real + tmp.real,this->imag + tmp.imag);
}
*/
Complex::Complex(int r)                                        
{
    this->real = r;
    this->imag = 0;
}

Complex Complex::operator ++()
{
    this->real = this->real + 1;
    this->imag = this->imag; 
    return *this;
}

Complex Complex::operator ++(int)
{
    this->real = this->real + 1;
    this->imag = this->imag; 
    return *this;
}

istream& operator>>(istream& input,Complex& tmp)         //对输入流的重载 
{
    input>>tmp.real>>tmp.imag;
    return input;
}

Complex operator +(Complex &tmp1,Complex &tmp2)            //使用类的友元函数进行“+”运算符的重载 (注意:友元是类的友元) 
{
    return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}

ostream& operator<< (ostream& output,Complex& tmp)         //对输出流的重载 
{
    output<<"["<<tmp.real<<"+"<<tmp.imag<<"i"<<"]";
    return output;
}

int main()
{
    int r = 10;
    Complex b(1,2);
    Complex a = 10 + b;  //本应隐示等价于Complex a = Complex(10) + b;
    cout<<a<<endl;
    getchar();
    return 0;
}

问程序段一中main函数内:Complex a = 10 + b;总报错~ 难道编译器不知道将10-》10+0i 再利用运算符重载实现复数加法吗?相反我的程序端二却又可以实现隐示转换,请问他们有什么区别呢? 谢谢~下面是程序段二
程序代码:
#include <iostream>

using namespace std;

class Complex
{
    public:
        ~Complex(){}
        Complex(int r,int i):real(r),imag(i){}
        Complex(int r){this->real = r;this->imag = 0;}
        operator int(){return real;}
        friend Complex operator + (Complex& tmp1,Complex& tmp2);
        
    private:
        int real;
        int imag;
};    

Complex operator + (Complex& tmp1,Complex& tmp2)
{
    return Complex(tmp1.real + tmp2.real , tmp1.imag + tmp2.imag);
}

int main()
{
    Complex a(1,2);
    Complex c = Complex(10);
    Complex b = a + 10; 
    getchar();
    return 0;
}
搜索更多相关主题的帖子: Complex 
2016-05-17 11:07
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
之前帮你改掉的那些错误竟然还在,有错不改,每次还问同样的错误有什么意义?!

其它恶心的错误不说了,仅就你提的这个问题,还是 const const const ……
friend Complex operator +(const Complex &tmp1,const Complex &tmp2);
Complex operator +(const Complex &tmp1,const Complex &tmp2)

BTW:既然用C++,却又不按C++规定的做,每每逆向而行,还问为什么
2016-05-17 11:58
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
这问题之前还答过一次 https://bbs.bccn.net/thread-457572-1-1.html
2016-05-17 12:21
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
回复 3楼 rjsp
谢谢! 这叫学了又忘,忘了又学!

既然还有不甘心
就还没到放弃的时候~
2016-05-18 22:33
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
回复 2楼 rjsp
我加了const但是还是不行~

既然还有不甘心
就还没到放弃的时候~
2016-05-21 18:08
S140131022
Rank: 2
来 自:重庆邮电大学
等 级:论坛游民
帖 子:205
专家分:35
注 册:2014-10-9
收藏
得分:0 
回复 4楼 S140131022
加了const可以了,谢谢大神! 我回头总结一下~

既然还有不甘心
就还没到放弃的时候~
2016-05-21 18:12
快速回复:关于转换构造函数隐示调用的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020963 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved