简体   繁体   中英

Is there a way to make a class member object's template call a different constructor?

In the class Matrix there is a member variable that is of type QArray<QArray<T>> . I am able to call the outside class QArray one param constructor with a member initializer list but for the inside class I am not sure how to do it. The inside QArray will use the default constructor first and then I will have to use a loop to make each matrix[i] use the 1 param constructor. Here is the code:

#include "QArray.h"
class Matrix {
    QArray<QArray<T>> matrix;

    Matrix(int n) : matrix(n) {
        for (int i = 0; i < n; i++) {
            matrix[i] = QArray<T>(n);
        }
    }
}

As a work around I changed the type to SA<SA<T> *> and changed matrix[i] = new SA<T>(cols) . This will prevent the default constructor from being called but I would like to write the class the way above.

Short answer : No .
You can't construct your QArray without using the constructors provided. In order to use a initialisation list there need to be a constructor which accept initialisation list. If we are talking about this QArray , then no, you can't.

Actually, why are you bothered about the default constructor be called? Qt is designed so that most objects can be default constructed then set. The default constructor is very lightweight.

You should be worried about the fact you are using a class defunct since 2005. You should also be worried to not be allowed to use nxn contiguous memory for your matrix.

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