具有模板引用联合继承类中的成员

Having a template refer to a member from a jointly-inherited class

本文关键字:继承 成员 引用      更新时间:2023-10-16

很明显,以下代码无法编译,因为它在"n = n_init"行处给出了"未声明的标识符"错误。尽管如此,对于人类读者来说,意图可能已经足够清楚了:我想为一个类声明一个模板,该类永远不会由自身实例化,而只能通过与另一个类一起进行多重继承,该类保证至少包含一个 int 类型的成员 'n' 和一个类型 T* 的成员 'p',但它(作为从其他地方获得的 C 结构)我不能自由地从另一个包含这些的模板派生领域:

// In a C header file from elsewhere:
// ----------------------------------
typedef struct {
    float *p;
    int n;
} float_array_C;
// In my C++ header file:
// ----------------------
template<typename T> class MyArray
{
public:
    MyArray(int n_init)
    {
        n = n_init;   
        contents.resize(n);
        p = contents.data();
    }
    virtual void mustExist() = 0;
private:
    std::vector<T> contents; 
};

class float_array : public float_array_C, public MyArray<float>
{
public:
    float_array(int n) : float_array_C(), MyArray(n)
    {}
    virtual void mustExist() {}
};
...
float_array testArray(10);

我也尝试过这种方法,但收效甚微:

typedef struct {
    float *p;
    int n;
} float_array_C;
template<typename T1, typename T2> class MyArray
{
public:
    MyArray(int n_init)
    {
        &T2::n = n_init;   
        contents.resize(n);
        &T2::p = contents.data();
    }
private:
    std::vector<T1> contents; 
};

typedef MyArray<float, float_array_C> floatArray;

...
float_array testArray(10);

这,或者任何与之类似的事情,实际上可以做到吗?

为了使它起作用,模板类必须派生自包含 n 的类型,然后您可以将其作为T::n访问,其中 T 是模板参数。

(不能仅使用 n 访问继承的成员,因为它不是依赖名称,因此编译器将在编译模板本身时尝试解析它,而不是稍后在实例化模板时解析它,并且MyArray或全局范围内不存在任何n。 使用 T::n 会导致它成为依赖名称(具体取决于T),因此名称的解析将延迟到模板实例化。

typedef struct {
    float *p;
    int n;
} float_array_C;
template <typename T>
class MyArray : public T
{
public:
    MyArray(int n_init) {
        T::n = n_init;
    }
};

请注意,您将遇到如下代码问题:

class Foo : public float_array_C, public MyArray<float_array_C> { /* ... */ };

在这种情况下,FooMyArray<float_array_C> 都包含单独的 float_array_C 实例。 如果存在此问题,您可以使用虚拟继承进行float_array_C

template <typename T>
class MyArray : virtual public T { /* ... */ };
class Foo :
    virtual public float_array_C,
    public MyArray<float_array_C>
{ /* ... */ };

另一种只需要一个模板参数的方法:

typedef struct {
    float *p;
    int n;
} float_array_C;
template<typename T> class MyArray : public T
{
public:
    MyArray(int n_init)
    {
        T::n = n_init;   
        contents.resize(T::n);
        T::p = contents.data();
    }       
private:
    std::vector<std::remove_pointer_t<decltype(T::p)>> contents;
};
typedef MyArray<float_array_C> floatArray;