Connecting Tech Pros Worldwide Forums | Help | Site Map

overload operator problem

Member
 
Join Date: Oct 2009
Posts: 33
#1: Oct 8 '09
hey there.
i'm having trouble to change my initial code to the one that uses stl vector.
i construct a new constructor vector<int>array to replace int*array.

but what about other steps? are there any major changes with the method if i'm trying to use stl vector?


Expand|Select|Wrap|Line Numbers
  1.  
  2. class My_Array 
  3. {
  4.  
  5.     int * array;
  6.     int count;
  7.  
  8. public:
  9.  
  10.     My_Array & operator = (const My_Array & other)
  11.     {
  12.         if (this != &other) 
  13.         {
  14.  
  15.             int * new_array = new int[other.count];
  16.             std::copy(other.array, other.array + other.count, new_array);
  17.  
  18.             delete [] array;
  19.  
  20.             array = new_array;
  21.             count = other.count;
  22.         }
  23.  
  24.         return *this;
  25.     }
  26.  
  27.  
  28. };
  29.  
  30.  
  31.  

i appreciate any help

Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#2: Oct 8 '09

re: overload operator problem


Your question isn't clear you talk about having changed from using int * accessed dynamic arrays to vector<int> but then you post code that hasn't had this done to it.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,384
#3: Oct 8 '09

re: overload operator problem


First, your My_Array::operator= doesn't work.

Since you return a reference, what you return must be exactly the original object but you have made a copy of the array so you don't have a reference.

You need to return *this.

But that means you need to make a My_Array object so you can return a referecne to that. Unfortunately, that will be a local object and you can't return a reference to a local object.

In short, your assignment operator needs to return a My_Array object and not a reference to one.

As to using vector<int>, C++ requires that vector<> implement a real array. Just like you are doing. This means that all of the vector<> member functions are direct replcements for the code you will write for My_Array. Plus yure code is non-standard and only works for the int type unless you make a template, which vector<> is already.

Therefore,

Expand|Select|Wrap|Line Numbers
  1. vector<int> obj;
completely replaces all of your code with standard code that everyone knows about.

So, unless this is some homework assignment, you should vector<> and never use native arrays or classes that you write that use native arrays.
Member
 
Join Date: Oct 2009
Posts: 33
#4: Oct 8 '09

re: overload operator problem


this is what i got so far

Expand|Select|Wrap|Line Numbers
  1. class My_Array  
  2.  
  3.     vector<int>array; 
  4.     int count; 
  5.  
  6. public: 
  7.  
  8.     My_Array & operator = (const My_Array & other) 
  9.     { 
  10.         if (this != &other)  
  11.         { 
  12.  
  13.             vector<int> new_array = new vector<int>; 
  14.             std::copy(other.begin(), other.end(), new_array); 
  15.  
  16.             //is there other way to delete vector<int>array without creating a nre pointer for it?
  17.             delete array; 
  18.   //does vector has it's own assigment because this part here didnt work.
  19.             array = new_array; 
  20.             count = other.count; 
  21.         } 
  22.  
  23.         return *this; 
  24.     } 
  25.  
  26.  
  27. }; 
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#5: Oct 8 '09

re: overload operator problem


int count is no longer required once you start using vector. vector has a member function that can return it's size.

Your if statement isoverly complex it can simplify to this

Expand|Select|Wrap|Line Numbers
  1.        if (this != &other)  
  2.         { 
  3.             array = other.array;
  4.         } 
  5.  
Member
 
Join Date: Oct 2009
Posts: 33
#6: Oct 8 '09

re: overload operator problem


if i do it that way can the operators be chained? like (A=B=C) ?
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#7: Oct 8 '09

re: overload operator problem


chaining is about the return value and parameters of the operator. It has nothing to do with how it is implemented, or rather it if the implementation is correct for A = B and you have the correct return value then it'll work in a chain too.
Member
 
Join Date: Oct 2009
Posts: 33
#8: Oct 8 '09

re: overload operator problem


thanks heaps for your help.now i get it. :)
Reply