0

I have a header file:

using namespace std;

class IntList{
private:
    int *Intl;
    int Capacity;
    int Count;
public:
    IntList(int capacity){
        Capacity = capacity;
        Count = 0;
        Intl = new int[capacity];
    }

    ~IntList(){
        delete Intl;
    }

    //adds the integers of the specified collection to the end of the List; return false if the new Count will be greater than Capacity
    bool AddRange(const IntList &items){
        //int *Temp = items.;
        if(items.Count > Capacity - Count){
            return false;
        }else{
            for(int i = 0; i <items.Count; i++){
                Intl[Count] = items.Intl[i];
                Count++;
            }
            return true;
        }
    }
};

But I don't know why I can't return value to IntList object in there:

//creates a copy of a range of elements in the source List
        IntList GetRange(int index, int count){
            IntList A(count);
            for(int i = 0; i < count; i++){
                A.Intl[i] = Intl[index -1 +i];
            }   
            return A;
        }

I want to return value of A whose type is IntList but I meet an error on "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in visual studio 2010. How can I repair it?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user1366448
  • 3
  • 1
  • 2

2 Answers2

3

Because int *Intl; is an object you manually manage, you'll need to implement the copy constructor for your class.

The function GetRange returns by value. The local object A gets destroyed, and its member Intl gets deleted in the destructor, so your copy (as copied by the default copy constructor) is only a shallow one, and will contain an invalid member.

EDIT: As Rob correctly pointed out, you'll also need to implement the assignment operator (you already have a destructor).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    See also [Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) and http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three. – Robᵩ Apr 30 '12 at 18:48
1

For an object which is returned by value makes a call to copy-constructor . You must make a copy-constructor and define it so that you get appropriate results. Returning by reference actually does not require call to copy constructor but should not be made for a temporary object. Also since you have a pointer type as member variable in class . It would be appropiate for you to overload the = operator. It should be define properly to avoid memory leak. Do something like this Intlist a=GetRange(index,count) . Also you should create a copy constructor for this . Your code also has a bug that it doesnot overload= operator for class Intlist .

you can write a copy constructor something like this :-

   Intlist::Intlist(const Intlist& cSource)
   {         
     capacity = cSource.capacity; 
     count=  cSource.count;
    // Intl is a pointer, so we need to deep copy it if it is non-null
    if (cSource.Intl)
     {
       // allocate memory for our copy
      Intl = new int[capacity];

    // Copy the Intl into our newly allocated memory in for loop
     for(i=0;i<capacity;i++)
     {
       // copy part
      }
    }
    else
    intl = NULL;
  }

Just an e.g how you should write it.

Invictus
  • 4,028
  • 10
  • 50
  • 80
  • @LuchianGrigore did not exactly meant to return temporary object by reference . I just wanted to let him know that in that case call to copy constructor would not be made – Invictus Apr 30 '12 at 19:37
  • @LuchianGrigore Modified it . Thanks for Suggestion :) – Invictus Apr 30 '12 at 19:38
  • I try anh fix it but it doesn't return value? – user1366448 May 01 '12 at 13:23
  • @user1366448 Did you write a copy constructor for it . If so can you post that code . Also post the code in Which you are calling GetRange function . – Invictus May 01 '12 at 17:35
  • // Copy contructor IntList (IntList &range){ Intl = new int; *Intl = *range.Intl; cout<<"copy complete\n\n"; } – user1366448 May 02 '12 at 14:12