模板继承另一个模板?

时间:2017-01-07 10:41:55

标签: c++

我试图做最大到最低的排序 我的Math Vector继承自Vector 现在问题是我无法看到inhert功能(见下文&&&&&)

template <class T>
class Vector
{
protected:
    T*      Array;
    int     Size;
    int     Capacity;

    public:


    Vector();
    Vector(Vector<T>& Copy_Array);

    T       operator=(const T* Element);
    void    operator=(const T Element);
    T*      operator[](const int Index) const;
   ostream& operator<<(const  Vector<T>& Print_Elements);


    void    Insert(T Element);/*Push*/
    void    ReSize(const int New_Size);
    T*      Pop();

    void    Set_size(int New_Size);
    void    Set_Capacity(int New_Capacity);

    int     Get_Size()     const;
    int     Get_Capacity() const;

    T*       Top()         const;

    ~Vector();
   };

这是继承只是函数ofcurse(protected)

的正确方法
 template<class T>
 class MathVector:public Vector<T>/*<- form template to tamplate*/
{ 
    public:
    void    Sort(const int Direction); 

};

&amp;&amp;&amp;&amp;&amp;&amp; / *我看不到矢量* /&amp;&amp;&amp;&amp;&amp;&amp;&amp;

的公共方法
template<class T>
void MathVector<T>::Sort(const int Direction)
{

this->  &&&&&&/* i can't see the public mathods of vector*/&&&&&&


};

1 个答案:

答案 0 :(得分:0)

是的,这是从Vector<T>继承的正确方法。并使用公共方法编写例如

template<class T>
void MathVector<T>::Sort(const int Direction)
{
    Vector<T>::yourNonPrivateMethod();
    auto test = Vector<T>::yourNonPrivateMember;
};

您也可以在头文件中通过using声明它们:

using Vector<T>::yourNonPrivateMethod;

修改 这是一个简化的例子:

#include <iostream>

template<typename T> class Base
{
public:
    Base() : member( static_cast<T>( 0 ) ) {};

    virtual ~Base() = default;

    T member;

    void baseMemberFunction()
    {
        std::cout << "Base<T>::memberFunction()" << std::endl;
    }

    void anotherBaseMemberFunction()
    {
        std::cout << "Base<T>::anotherBaseMemberFunction()" << std::endl;
    }
};


template<typename T> class Derived : public Base<T>
{
public:
    Derived() : Base<T>(), member(1) {}

    T member;

    using Base<T>::anotherBaseMemberFunction;

    void derivedMemberFunction()
    {
        std::cout << "Derived<T>::derivedMemberFunction()" << std::endl;
        Base<T>::baseMemberFunction();
        anotherBaseMemberFunction();
        std::cout << "Derived<T>::member = " << member << std::endl;
        std::cout << "Base<T>::member = " << Base<T>::member << std::endl;
    }
};


int main()
{
    Derived<int> derived;

    std::cout << " --- Call baseMemberFunction --- " << std::endl;
    derived.baseMemberFunction();

    std::cout << " --- Call derivedMemberFunction --- " << std::endl;
    derived.derivedMemberFunction();

    return 0;
}