返回一个类的引用

时间:2014-02-07 12:58:41

标签: c++ class methods reference

我在理解c ++中的引用时遇到了一些问题,尤其是在从方法返回类的引用时。  像下面的代码完美地工作:(粗体部分是问题所在)

    #include<iostream>
    using namespace std;
    class Sequence
    {
        int *a;
        int length;

        void allocMemory()
        {
            this->a = new int[this->length];
        }

        void fill(int val)
        {
            for (int i = 0; i<this->length; i++) this->a[i] = val;
        }
    public:
        Sequence(int len=0)
        {
            this->length=len;
            this->allocMemory();
            this->fill(0);
        }
        Sequence(int data,int len)
        {
            this->length=len;
            this->allocMemory();
            this->fill(data);
        }
        Sequence(int *data,int len)
        {
            this->length=len;
            this->allocMemory();
            for(int i=0;i<this->length;i++) this->a[i]=data[i];
        }
        Sequence(const Sequence &s)
        {
            length=s.length;
            allocMemory();
            for(int i=0;i<length;i++){
                a[i]=s.a[i];
            }
        }
        ~Sequence()
        {
            delete [] this->a;
        }

        friend ostream & operator<<(ostream &stream, Sequence &s)
        {
            stream << "Sequence: " ;
            for(int i=0;i<s.length;i++) stream <<  s.a[i] << " ";
            stream << "  Length = " << s.length << endl;
            return stream;
        }

        friend istream & operator>>(istream &stream, Sequence &s)
        {
            int n;
            cout << "No of elements:";
            stream >> n;
            s.length=n;
            s.allocMemory();
            cout << "Enter the elements:";
            for(int i=0;i<n;i++) stream >> s.a[i];
            return stream;
        }
        Sequence &operator= (int data){ // this method works fine as i return a reference
            for(int i=0;i<length;i++){
                a[i]=data;
            }
            return *this;
        }
        **Sequence operator+(Sequence ob){ 
            /*this is the problematic method. It works fine this way. as i already got 
             the necessary copy constructor code. But if i change the return value to a      
             reference like the sample code within the comment, the program doesn't give the correct output. Though as per my theoretical knowledge, i can't find any problem in this code which too should run perfectly even if the copy constructor code was missing.
  Sequence & operator+(Sequence ob){
        int i,j;
        int l=length+ob.length;
        Sequence temp(0,l);
        for(i=0;i<length;i++){
            temp.a[i]=a[i];
        }
        for(j=0;i<l || j<ob.length;i++,j++){
            temp.a[i]=ob.a[j];
        }
        return temp;
    }
        */**
            int i,j;
            int l=length+ob.length;
            Sequence temp(0,l);
            for(i=0;i<length;i++){
                temp.a[i]=a[i];
            }
            for(j=0;i<l || j<ob.length;i++,j++){
                temp.a[i]=ob.a[j];
            }
            return temp;
        }
        void show(){
            for(int i=0;i<length;i++){
                cout<<a[i]<<" ";
            }
        }
    };
    int main(){
        int arr[]={1,2,3,4,5,6,7,8,9,10};
        Sequence a(arr,10);
        cout << a;
        Sequence b(10);
        cout << b;
        cin >> b;
        cout << b;
        Sequence c=a+b;
        c.show();
    }

1 个答案:

答案 0 :(得分:6)

您不能通过引用返回临时值 - 您将在函数外部留下悬空引用,因为该函数在函数结束时将被销毁。

惯用法,operator+按值返回。相比之下,operator+=通过引用返回(以启用链接),但是您将返回*this,这不是临时的并使其有效。