This will go faster if I show you what I want to do.

class matrix {
public:
  double *x;
  int clms, rows;

  matrix(int unknowns)
  {
    rows = unknowns;
    clms = unknowns + 1;
    x = new double[rows * clms];
  }

  matrix operator = (double a[]) const
  {
    matrix tmp(rows);
    
    tmp.x = a;
    return tmp;
  }     
};



int main()
{
  matrix m(3) = { 1, 2, 3.2, 4,
                  3, .2, 1, 4,
                  5, -2, .68, 5};
}

basically I want to be able to initialize and fill up m.x with the array that I passed it.
This is basically what the string class lets you do, for example:
string s = { 'h', 'e', 'l', 'l', 'o' , '\0' };
or as normal people do:
string s = "hello";

Anyone know how to do this?

Dani AI

Generated

was on the right track wanting brace-init support, but implementing operator= that takes a raw double[] won't do what you expect (arrays decay to pointers, no size info, and assignment operators must not be const). The modern, safe way is to accept brace-init lists via std::initializer_list (or provide a constructor that takes one) and use a container like std::vector for automatic memory management.

A compact, practical pattern:

#include <vector>
#include <initializer_list>
#include <algorithm>
#include <stdexcept>

class matrix {
    int rows, cols;
    std::vector<double> data;
public:
    explicit matrix(int r) : rows(r), cols(r+1), data(rows*cols) {}

    matrix(int r, std::initializer_list<double> init)
      : rows(r), cols(r+1), data(init)
    {
        if (data.size() != static_cast<size_t>(rows*cols))
            throw std::length_error("initializer size mismatch");
    }

    matrix& operator=(std::initializer_list<double> init) {
        if (init.size() != data.size()) throw std::length_error("assign size mismatch");
        std::copy(init.begin(), init.end(), data.begin());
        return *this;
    }
};

Use it as either matrix m(3, { /* 12 numbers */ });, or construct then assign matrix m(3); m = { /* 12 numbers */ };. The form matrix m(3) = { … }; will not pass the 3 and the list together to a constructor—prefer the two forms above or matrix m{3, { … }};.

A few practical tips:

  • Prefer std::vector (RAII) to raw new[] so you don't have to write copy/move/destructor logic.
  • Always validate the initializer size (throw or assert) to avoid silent memory errors.
  • If you need assignment from raw arrays, pass a pointer plus a length, or copy into a std::span/std::vector.

For details on brace initialization and std::initializer_list, see the reference on std::initializer_list and on std::vector. ’s copy-constructor link is useful background, but the brace-init approach above is what enables the { ... } syntax you wanted.

Recommended Answers

All 2 Replies

Take a look at http://www.deitel.com/articles/cplusplus_tutorials/20060204/index.html

SUMMARY:
This tutorial introduces a copy constructor for initializing a new Array object with the contents of an existing Array object. This tutorial is intended for students and professionals who are familiar with basic array, pointer and class concepts in C++.

Yes but I'm not going to pass another matrix class object to the = operator, instead I want to initialize the member x array as I would a regular array, like this:
matrix m(3) = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ,12};

and not do this:

matrix m(3), m2(3);
for (int i = 0; i < 12; i++)
m.x = i + 1;
m2 = m;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.