user3706129
user3706129

Reputation: 229

Returning *this

I have a class which contains a array of some int. I want to overload * operator so it would multiplies every element by certain number. Class is very common.

class TEST{
public:
      TEST(){
        size = 5;
        arr = new double[size]();
      }
      double &operator [] ( const int a ){
        return arr[a];
      }

      TEST operator *( const int a ){
         for( int i = 0; i < size ;i++){
             arr[i] = arr[i] * a;
        }
        return *this;
     }   
private:
     int size;
     double *arr;
}

When i invoke it as

TEST one;
one[1]=5;
one[0]=-10;
one[3]=4
one = one * 2

and printing it , the value of result , the 0 index is always random number from memory , like it does not have 0 index ; this happens only after one = one * 2. Is *this causing this? If so why and how can i fix it?

// I have declared * as

TEST operator *( const int a ){
       TEST temp;
       for( int i = 0; i < size ;i++){
           temp.arr[i] = arr[i] * a;
       }
           return temp;
}

and declared copy constructor

TEST( const TEST &a){
        size    = a.size;
        arr     = new double;
        *arr    = *a.arr;
    }

And it still throws random nubmer as 0 index

Upvotes: 1

Views: 85

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

The subscript operator should be defined the following way

  double & operator [] ( int a ){
    return arr[a];
  }

and

  double operator [] ( int a ) const {
    return arr[a];
  }

or

  const double & operator [] ( int a ) const {
    return arr[a];
  }

operator * should be defined the following way

     TEST operator *( int a ) const {
         TEST tmp;
         for( int i = 0; i < size ;i++){
             tmp.arr[i] = arr[i] * a;
        }
        return tmp;
     }   

Or the declaration can look also like

const TEST operator *( int a ) const;
^^^^^

You need also to define at least (apart from the destructor and the copy constructor) the copy assignment operator explicitly.

Here is a demonstrative program

#include <iostream>

class TEST
{
public:
    TEST() : size( 5 ), arr( new double[size]() )
    {
    }

    ~TEST()
    {
        delete []arr;
    }        

    TEST & operator =( const TEST &rhs )
    {
        for( int i = 0; i < size ;i++ ) arr[i] = rhs.arr[i];

        return *this;
    }             

    double & operator [] ( int n )
    {
        return arr[n];
    }

    const double & operator [] ( int n ) const 
    {
        return arr[n];
    } 

    TEST operator *( int x ) const 
    {
        TEST tmp;

        for ( int i = 0; i < size ;i++ )
        {
            tmp.arr[i] = x * arr[i];
        }

        return tmp;
    }

    int count() const
    {
        return size;
    }        
 private:
    int size;
    double *arr;
};

int main()
{
    TEST one;
    one[1] = 5;
    one[0] =-10;
    one[3] = 4;
    one = one * 2;

    for( int i = 0; i < one.count() ;i++ ) std::cout << one[i] << ' ';
    std::cout << std::endl;
}    

Its output is

-20 10 0 8 0 

EDIT: It is a bad idea that you changed your post after my answer using the code shown in my answer. Your copy constructor is invalid. It creates only one object of type double instead of an array.

Upvotes: 3

Related Questions