equal operator is overloading

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdlib>
#include <iostream>

using namespace std;

class Array 
{
      private:
        int* ptr;
        int size;
      
      public:
        Array(int s)
        {
            size = s;
            ptr = new int[s];
        }
        
        ~Array()
         {  
                delete[] ptr;  
         }
         
         int& operator [] (int j)
         {
              return *(ptr+j);
         }           
         
         Array operator = (Array& dizi)
         {     

               ptr = dizi.ptr;
              return Array(ptr);
         }
};
                  
int main(int argc, char *argv[])
{
    const int ASIZE = 10;
    Array arr(ASIZE);
    
    
    for(int i=0; i<ASIZE; i++)
     arr[i] = i*i;
     
    for(int i=0; i<ASIZE; i++)
     cout << arr[i] << " " ;
     cout << endl;
     

    system("PAUSE");
    return EXIT_SUCCESS;
}

i take this error
In member function `Array Array::operator=(Array&)':
invalid conversion from `int*' to `int'
33.line
line 33 return Array(ptr); constructs an Array object by passing 'ptr' to the ctor. Your ctor takes an int, but 'ptr' is an int*, which is why you get the error.

However this is just a tangent of your real problem: your assignment operator is a bit flawed:

1) it should take a const reference as the param, not a non-const (ie: const Array& dizi).

2) it should return a reference to 'this', and should not construct an entirely separate object.

3) if should make a copy of the contained array, rather than simply duplicating the pointer. Consider that if you have two Array objects 'a' and 'b', and assign a=b; Then when a's destructor runs it will delete[] the buffer, and when b's destructor runs it will delete[] the same buffer (since they share the same pointer) -- this will most likely cause your program to crash.

4) The same problem as #3 will happen with the default copy ctor, which you should overload as well.

5) You'll need to protect against self-assignment (ie: "a=a;").


Here's a more typical/functional assignment operator (note there are optimizations you can make, but I'm trying to keep the example simple):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Array& operator = (const Array& dizi)
{
  // protect against self assignment
  if(&dizi == this)
    return *this;

  // kill the original buffer
  delete[] ptr;

  // re-allocate the buffer
  size = dizi.size;
  ptr = new int[size];

  // copy the buffer contents
  for(int i = 0; i < size; ++i)
    ptr[i] = dizi.ptr[i];

  // all done -- return reference to this
  return *this;
}


The copy ctor can make use of the assignment operator -- just be careful about that delete[]! Don't want to delete a bad pointer:

1
2
3
4
5
Array(const Array& dizi)
{
  ptr = 0;  // make it a null pointer so delete[] has no effect
  *this = dizi;  // then copy
}
you are cool. very helpful thank u so much
Topic archived. No new replies allowed.