簡體   English   中英

繼承自 boost::matrix

[英]Inherit from boost::matrix

我想從boost::matrix繼承來豐富一些方法。 我從這個開始:

#include <boost/numeric/ublas/matrix.hpp>

using namespace boost::numeric::ublas;

class MyMatrix : public matrix<double>
{
public:
    MyMatrix() : matrix<double>(0, 0) {}

    MyMatrix(int size1, int size2) : matrix<double>(size1, size2) {}

    MyMatrix(MyMatrix& mat) : matrix<double>(mat) {}

    MyMatrix(matrix<double>& mat) : matrix<double>(mat) {}

    MyMatrix& operator=(const MyMatrix& otherMatrix)
    {
        (*this) = otherMatrix;
        return *this;
    }
};

這讓我可以做這樣的事情:

MyMatrix matA(3, 3);
MyMatrix matB(3, 3);
MyMatrix matC(matA);

但我可能會錯過一些東西,因為我無法做到:

MyMatrix matD(matA * 2);
MyMatrix matE(matA + matB);

這導致:

error: conversion from 'boost::numeric::ublas::matrix_binary_traits<boost::numeric::ublas::matrix<double>, boost::numeric::ublas::matrix<double>, boost::numeric::ublas::scalar_plus<double, double> >::result_type {aka boost::numeric::ublas::matrix_binary<boost::numeric::ublas::matrix<double>, boost::numeric::ublas::matrix<double>, boost::numeric::ublas::scalar_plus<double, double> >}' to non-scalar type 'MyMatrix' requested

如何使用boost::matrix中的方法而不在MyMatrix中重新定義所有方法?

您不需要任何添加即可完成此工作:

MyMatrix matA(3, 3);
MyMatrix matB(3, 3);
MyMatrix matC(matA);

MyMatrix matD(matA * 2);
MyMatrix matE(matA + matB);

您只需要將boost::numeric::ublas::matrix<double>構造函數和賦值運算符帶入派生的 class 中:

#include <boost/numeric/ublas/matrix.hpp>

class MyMatrix : public boost::numeric::ublas::matrix<double> {
public:
    using matrix<double>::matrix;    // use the constructors already defined
    using matrix<double>::operator=; // and the operator=s already defined

    // put your other additions here (except those you had in the question)
};

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM