简体   繁体   中英

second Constructor called in c++ ( wrong output)

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

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM