Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: base class should be explictily initialized in the copy constructor

I'm writing a matrix class for CUDA processing.

I've written a vector class (henceforth known as Elements) and used that for the matrix base.

Here is the template definition:

template <typename T, std::size_t M, std::size_t N>
class Matrix : public Elements< Elements< T, N >, M > {

}

It should be noted that nothing is dynamically allocated in the Elements class, nor in the Matrix class.

I'm getting a warning: base class ‘struct Elements<Elements<double, 2ul>, 2ul>’ should be explicitly initialized in the copy constructor warning in the copy constructor. Here is the copy constructor:

    DEVICE HOST
    Matrix(const Matrix & that) {
        for (std::size_t ind = 0; ind < M; ind++) {
            for (std::size_t jnd = 0; jnd < N; jnd++) {
                (*this)[ind][jnd] = that[ind][jnd];
            }
        }
    }

What am I doing wrong?

like image 512
Tyler Jandreau Avatar asked Jan 15 '14 14:01

Tyler Jandreau


1 Answers

You're not initializing the base class in the copy constructor. Try this:

Matrix(const Matrix & that) : Elements<Elements<T, N>, M>(that) {
    /* ... */
}

The initializer list of the derived class' copy constructor should contain an explicit call to the base class' copy constructor, just like with all other constructors, otherwise, the base class will be default-initialized.

Edit: It can be handy to have a private

typedef Elements<Elements<T, N>, M> basetype;

in your class definition somewhere.

like image 160
arne Avatar answered Nov 02 '22 07:11

arne