geogoe12
geogoe12

Reputation: 49

How can i dynamicly create classes and also use the overloaded constructor?

I want to create a matrix of complex numbers. Point is that i want to create the matrix dynamically. I am stuck when i want to call the overloaded constructor.

I have 2 classes: - nrComplex representing the complex numbers; - mMatrix representing the matrix of complex numbers;

In the code i first create the rows then i want to create the columns for every row but i dont know how to initialize the class using the overloaded constructor

Any idea? Thanks

Ive done some research but i couldnt find anything that can fit my needs

class nrComplex
{
private:
    float mReala, mImaginara;
public:
    nrComplex ();
    nrComplex (float, float);

    friend class mMatrix;

};

nrComplex::nrComplex()
{
    mReala = 0;
    mImaginara = 0;
}

nrComplex::nrComplex(float a, float b)
{
    mReala = a;
    mImaginara = b;
}
class mMatrix
{
private:
    int rows, columns;
    nrComplex **matrice;

public:
    mMatrix ();
    mMatrix (float, float);

};

mMatrix::mMatrix()
{
    rows = 0;
    columns = 0;
    matrice = NULL;
}

mMatrix::mMatrix (float n, float m)
{
    rows = n;
    columns = m;

    matrice = new nrComplex*[rows];

    for(int i=0;i<rows;i++)
    {
        matrice[i] = new nrComplex[columns];
    }

   // here is the part ^^^ where i get stuck
}

Upvotes: 1

Views: 46

Answers (1)

Wilfred Smith
Wilfred Smith

Reputation: 154

The square brackets will cause you to create an array of nrComplex objects. They will be initialized using the default constructor. If you want to re-initialize the nrComplex object, you can use placement new. I don't think you can create an array of objects and call anything other than the default constructor at the same time.

Since you're okay with friend'ing, you could just set the internal variables

Upvotes: 1

Related Questions