从c ++中的模板类继承 [英] Inheriting from a template class in c++

查看:107
本文介绍了从c ++中的模板类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个模板类 Area ,它有一个成员变量 T area T getArea()和一个 void setArea(T)成员函数。

Let's say we have a template class Area, which has a member variable T area, a T getArea() and a void setArea(T) member functions.

我可以通过键入 Area< int> 来创建特定类型的 Area 对象。

I can create an Area object of a specific type by typing Area<int>.

现在我有一个继承 Area 类的类 Rectangle 。由于 Rectangle 本身不是一个模板,我不能键入 Rectangle< int>

Now I have a class Rectangle that inherits the Area class. Since Rectangle itself is not a template, I cannot type Rectangle<int>.

如何专门为 Rectangle 对象继承的 Area 类型?

How do I specialize the inherited Area type for Rectangle objects?

编辑:对不起,我忘记澄清了 - 我的问题是是否可能继承没有专门化的区域,所以它不继承作为区域的int,但作为区域矩形可以专门化类型for。

Sorry, I forgot to clarify - my questions is whether it is possible to inherit Area without specializing it, so it is not inherited as Area of ints but as Area Rectangle can specialize the types for.

推荐答案

为了理解模板,使用术语具有巨大的优势,因为你谈论它们的方式决定了

For understanding templates, it's of huge advantage to get the terminology straight because the way you speak about them determines the way to think about them.

具体来说, Area 不是模板类,而是类模板。也就是说,它是一个可以生成类的模板。 Area< int> 是这样一个类(它不是一个对象,但当然你可以用同样的方式创建一个对象)可以从任何其他类创建对象)。另一个这样的类将是 Area< char> 。注意,这些是完全不同的类,除了它们是从相同的类模板生成的事实之外没有什么共同之处。

Specifically, Area is not a template class, but a class template. That is, it is a template from which classes can be generated. Area<int> is such a class (it's not an object, but of course you can create an object from that class in the same ways you can create objects from any other class). Another such class would be Area<char>. Note that those are completely different classes, which have nothing in common except for the fact that they were generated from the same class template.

由于 不是类,不能从它派生类 Rectangle 。你只能从另一个类(或其中的几个)派生一个类。由于 Area< int> 是一个类,因此您可以从中获取 Rectangle

Since Area is not a class, you cannot derive the class Rectangle from it. You only can derive a class from another class (or several of them). Since Area<int> is a class, you could, for example, derive Rectangle from it:

class Rectangle:
  public Area<int>
{
  // ...
};

由于区域< int> c $ c> Area< char> 是不同的类,你甚至可以同时从两者派生(但是当访问它们的成员时,你必须处理歧义):

Since Area<int> and Area<char> are different classes, you can even derive from both at the same time (however when accessing members of them, you'll have to deal with ambiguities):

class Rectangle:
  public Area<int>,
  public Area<char>
{
  // ...
};


$ b 。无论这些类是从模板生成还是不生成,这都是真的。同一个类的两个对象根本不能有不同的继承层次。

However you have to specify which classed to derive from when you define Rectangle. This is true no matter whether those classes are generated from a template or not. Two objects of the same class simply cannot have different inheritance hierarchies.

你可以做的是使 Rectangle a模板。如果你写

What you can do is to make Rectangle a template as well. If you write

template<typename T> class Rectangle:
  public Area<T>
{
  // ...
};

您有一个模板 Rectangle 可以得到源自 Area 的一个类 Rectangle< int> ,以及不同的类 Rectangle< char> 它源于区域< char>

You have a template Rectangle from which you can get a class Rectangle<int> which derives from Area<int>, and a different class Rectangle<char> which derives from Area<char>.

可能是你想要一个类型 Rectangle ,以便你可以传递各种 Rectangle 到同一个函数(它本身不需要知道Area类型)。由于通过实例化模板 Rectangle 生成的 Rectangle< T> 类在形式上彼此独立,工作方式。但是你可以在这里使用多重继承:

It may be that you want to have a single type Rectangle so that you can pass all sorts of Rectangle to the same function (which itself doesn't need to know the Area type). Since the Rectangle<T> classes generated by instantiating the template Rectangle are formally independent of each other, it doesn't work that way. However you can make use of multiple inheritance here:

class Rectangle // not inheriting from any Area type
{
  // Area independent interface
};

template<typename T> class SpecificRectangle:
  public Rectangle,
  public Area<T>
{
  // Area dependent stuff
};

void foo(Rectangle&); // A function which works with generic rectangles

int main()
{
  SpecificRectangle<int> intrect;
  foo(intrect);

  SpecificRectangle<char> charrect;
  foo(charrect);
}



如果重要的是你的泛型 Rectangle 源自一个通用的 Area ,你也可以用 Area 做同样的技巧:

If it is important that your generic Rectangle is derived from a generic Area you can do the same trick with Area too:

class Area
{
  // generic Area interface
};

class Rectangle:
  public virtual Area // virtual because of "diamond inheritance"
{
  // generic rectangle interface
};

template<typename T> class SpecificArea:
  public virtual Area
{
  // specific implementation of Area for type T
};

template<typename T> class SpecificRectangle:
  public Rectangle, // maybe this should be virtual as well, in case the hierarchy is extended later
  public SpecificArea<T> // no virtual inheritance needed here
{
  // specific implementation of Rectangle for type T
};

这篇关于从c ++中的模板类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆