0

I am trying to do a simple thing but suddenly stuck in between . Here in my code I am trying to call a constructor in which i would only pass the length, my first constructor initializes an array of size = length with all elements 0.

Then i am passing the array to the constructor to give the previously defined array its values

here is the sample :

class myvector
{
  int *arr;
  int length;

public :
    myvector(int);
    myvector(int *);

};

myvector :: myvector (int len)
{
    arr =  new int [length = len];
    for ( int i=0;i< length;i++)
    {
        arr[i] = 0;
    }

}

myvector :: myvector ( int *ex)
{
    for ( int i=0;i< length;i++)
    {

        cout << ex[i] << " " << length <<" ";
        arr[i] = ex[i];
        cout << arr[i]<< " ";

    }
}

int main()
{

    myvector v1(5);
    int x[5] = {2,3,4,45,6};
    v1 = x;

}

Here in my second constructor length which was defined in first constrcutor lost its values , also array arr loses its values

Did I do something ? Please elaborate me on this

Ke7in
  • 917
  • 2
  • 7
  • 16
  • First of all, you never allocate any memory for `arr` the `v` array dies right after the end of the constructors scope, you'll have an undefined behavior... (same goes for the second constructor which just writes to unallocated memory) – Scis Aug 03 '14 at 10:35
  • why don't just use [std::vector](http://www.cplusplus.com/reference/vector/vector/)? – user1810087 Aug 03 '14 at 10:38
  • @Scis i have edited to give memory but still the same error even length loses value – Ke7in Aug 03 '14 at 10:47
  • 1
    On your second constructor call in your main method, you create a new object of `myvector` which has the initial value `length=0`. The length you saved bevore is not available for your new instance. – msrd0 Aug 03 '14 at 10:57

2 Answers2

4

I don't think you quite get what the circumstances are in which constructors are invoked. The line v1 = x doesn't put the values into the memory allocated during the first constructor call. Rather, it constructs a new myvector (using the second constructor) and copies it into v1. The stuff you do during the first constructor call is lost.

It sounds like you want to define an assignment operator, not a constructor taking an int* argument. Also, in general you should declare single-argument constructors as explicit to prevent this sort of thing from happening accidentally.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
2

First of all, when specifying a size of array like this, you should provide a constant value. See here. And I really encourage you to use std::vector<> for such tasks.

But anyway, like Sneftel mentioned, seems like you want to define an assignment operator (see here for more information about overloading operators in tasks like yours).

Here how I'd implemented it(NOT and ideal solution, but it works, and gives a basic idea):

#include <iostream>

using namespace std;

class myvector
{
  int *arr;
  int length;

public :
    //I completely removed second constructor 
    myvector(int);
    ~myvector();

    void operator=(const int* otherArray);
    void printArray();
};

myvector::myvector (int len)
{
    length = len;

    arr = new int[length]; // This is the way, how you can handle a non constant array sizes

    for ( int i=0;i< length;i++)
    {
        arr[i] = 0;
    }
}

void myvector::printArray()
{
    for(unsigned i = 0; i < length; ++i)
        cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}

//Here's an overloaded '=' operator.
//int x[5] = {2,3,4,45,6};
//myvector v;
//v = x; - here this function is called
void myvector::operator=(const int* otherArray)
{
    for(unsigned i = 0; i < length; ++i)
        arr[i] = otherArray[i];
}

myvector::~myvector()
{
    delete []arr; // You should ALWAYS delete what you allocated with new
}

int main()
{
    myvector v1(5);
    int x[5] = {2,3,4,45,6};

    v1 = x;

    v1.printArray();
}
Community
  • 1
  • 1
Russel Ledge
  • 221
  • 3
  • 11