从一个模板类转换到另一个(两者都派生自同一基类)



我四处寻找,但找不到问题的答案。一段时间以来,我一直在尝试制作一个转换构造函数来在我的派生类中工作,但无论我做什么都无法使其工作。

简单地说,我有一个抽象基类和两个(很快就会有更多)派生类,它们都使用模板(它们是向量,所以它们使用模板来接受多种类型的元素)。一个是向量,其大小由模板参数决定,另一个的大小取决于用于创建对象的参数。

我希望能够使用转换构造函数从一个转换到另一个。以下是这两个类的声明。我认为我应该跳过这些功能的实现,因为它们很长,在我看来与问题无关,但如果你认为有必要,我会将其添加到其他功能中。

这是动态矢量类

template <class elem>
class Vect_variable : public Vect<elem>
{
    template <class T>
        friend std::ostream& operator<< (std::ostream&, const Vect_variable<T>&);
    template <class T>
        friend Vect_variable<T>& operator+ (T, const Vect_variable<T>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x
    template <class T>
        friend Vect_variable<T>& operator- (T, const Vect_variable<T>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x
    template <class T>
        friend Vect_variable<T>& operator* (T, const Vect_variable<T>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x
    public:
        Vect_variable(std::size_t sz = 0): taille(sz), vecteur(new elem[sz]) {};
        Vect_variable(std::size_t, const elem&);
        Vect_variable(const Vect_variable&);
        Vect_variable(Vect_variable&&);
        Vect_variable& operator=(const Vect_variable&);
        virtual elem& operator[] (std::ptrdiff_t nIndex);
        virtual const elem& operator[] (std::ptrdiff_t nIndex) const;
        virtual Vect_variable& operator+(); // + unaire
        virtual Vect_variable& operator-(); // - unaire
        virtual Vect_variable& operator+(const elem&); // + compos. par compos.
        virtual Vect_variable& operator-(const elem&); // - compos. par compos.
        virtual Vect_variable& operator*(const elem&); // * compos. par compos.
        virtual Vect_variable& operator+=(const elem&); // + compos. par compos.
        virtual Vect_variable& operator-=(const elem&); // - compos. par compos.
        virtual Vect_variable& operator*=(const elem&); // * compos. par compos.
        Vect_variable& operator+(const Vect_variable&); // + vect dynam.
        Vect_variable& operator-(const Vect_variable&); // - vect dynam.
        Vect_variable& operator*(const Vect_variable&); // * vect dynam.
        Vect_variable& operator+=(const Vect_variable&); // + vect dynam.
        Vect_variable& operator-=(const Vect_variable&); // - vect dynam.
        Vect_variable& operator*=(const Vect_variable&); // * vect dynam.
        ~Vect_variable () {delete[] vecteur ;}
    private:
        elem* vecteur;
        std::size_t taille;
};

这是一个向量,其大小由模板参数固定

template <class elem, std::size_t taille=10>
class Vect_fixe: public Vect<elem>
{
    template <class T, std::size_t D>
        friend std::ostream& operator<< (std::ostream&, const Vect_fixe<T, D>&);
    template <class T, std::size_t D>
        friend Vect_fixe<T,D>& operator+ (T, const Vect_fixe<T, D>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x
    template <class T, std::size_t D>
        friend Vect_fixe<T,D>& operator- (T, const Vect_fixe<T, D>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x
    template <class T, std::size_t D>
        friend Vect_fixe<T,D>& operator* (T, const Vect_fixe<T, D>&);
        // Pour les operations x+vecteur et pas vecteur+x avec int x

    public:
        Vect_fixe() = default;
        Vect_fixe(const elem&);
        virtual elem& operator[] (std::ptrdiff_t nIndex);
        virtual const elem& operator[] (std::ptrdiff_t nIndex) const;
        virtual Vect_fixe& operator+() ; // + unaire
        virtual Vect_fixe& operator-(); // - unaire
        virtual Vect_fixe& operator+(const elem&); // + compos. par compos.
        virtual Vect_fixe& operator-(const elem&); // - compos. par compos.
        virtual Vect_fixe& operator*(const elem&); // * compos. par compos.
        virtual Vect_fixe& operator+=(const elem&); // + compos. par compos.
        virtual Vect_fixe& operator-=(const elem&); // - compos. par compos.
        virtual Vect_fixe& operator*=(const elem&); // * compos. par compos.
        Vect_fixe& operator+(const Vect_fixe&); // + vecteur meme taille
        Vect_fixe& operator-(const Vect_fixe&); // - vecteur meme taille
        Vect_fixe& operator*(const Vect_fixe&); // * vecteur meme taille
        Vect_fixe& operator+=(const Vect_fixe&); // + vecteur meme taille
        Vect_fixe& operator-=(const Vect_fixe&); // - vecteur meme taille
        Vect_fixe& operator*=(const Vect_fixe&); // * vecteur meme taille
        std::size_t get_size();
        elem get_vecteur();
    private:
        elem vecteur[taille] = {0};
        std::size_t size = taille;
};

因此,我尝试使用一个接受其他类类型参数的构造函数,但这似乎不起作用,因为其他类的一些模板参数没有声明(?),如果我声明它们,它只会一直说无效的模板参数。我玩了很多,但我想不出正确的方法。我对互联网缺乏丰富的知识(或者可能我自己的研究能力也缺乏),但我被卡住了。

无论如何,这背后的总体想法是使用某种东西:

Vect_variable(Vect_fixe<elem, taille>)

或者最终以任何其他方式从一种类型转换为另一种类型。

正向声明,可能需要两个级别:

#include <cstdint>
template<class Elem>
struct base
{
};
// forward declare classes
template<class Elem> struct variable;
template<class Elem, size_t N> struct fixed;
//forward declare members
template<class Elem> struct variable : base<Elem>
{
    variable() = default;
    template<size_t N> variable(const fixed<Elem, N>& r);
    variable& operator += (const variable& r);
    template<size_t N>
    variable& operator += (const fixed<Elem, N>& r);
};
template<class Elem, size_t N> struct fixed : base<Elem>
{
    fixed() = default;
};
// define members
template<class Elem>
template<size_t N>
variable<Elem>::variable(const fixed<Elem, N>& r)
{
    // implementation here
}
template<class Elem>
auto variable<Elem>::operator += (const variable& r) -> variable&
{
    // implementation here
    return *this;
}
template<class Elem>
template<size_t N>
auto  variable<Elem>::operator += (const fixed<Elem, N>& r) -> variable&
{
    // implementation here
    return *this;
}

using namespace std;
auto main() -> int
{
    fixed<double, 10> fd;
    variable<double> vd(fd);
    vd += fd;
    return 0;
}

我看不到从Vect_variable转换为Vect_fixe的方法,后者需要将大小作为模板参数(因此在编译时是恒定的),但获得Vect_variable大小的唯一方法是在运行时。

但我认为从Vect_fixe转换为Vect_variable:没有任何特别的问题

template<std::size_t size> Vect_variable(const Vect_fixe<elem,size>& other) : Vect_variable(size)
{ 
  for (int i = 0; i < size; ++i)
  {
    (*this)[i] = other[i];
  }
}

相关内容

最新更新