STL化C ++类 [英] STLifying C++ classes

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

问题描述

我想写一个包含多个std :: vector作为数据成员的类,并提供一个向量接口的子集来访问它们:

I'm trying to write a class which contains several std::vectors as data members, and provides a subset of vector's interface to access them:

class Mesh
{
public:
private:
  std::vector<Vector3> positions;
  std::vector<Vector3> normals;
  // Several other members along the same lines
};

你可以用网格做的主要事情是添加位置,法线和其他东西。为了允许类似STL的方式访问Mesh(从数组,其他容器添加等等),我想要添加这样的方法:

The main thing you can do with a mesh is add positions, normals and other stuff to it. In order to allow an STL-like way of accessing a Mesh (add from arrays, other containers, etc.), I'm toying with the idea of adding methods like this:

public:
  template<class InIter>
  void AddNormals(InIter first, InIter last);

问题是,从我理解的模板,这些方法必须在头文件(似乎有意义;没有一个具体的迭代器类型,编译器不知道如何生成目标代码为这个方法的明显实现)。

Problem is, from what I understand of templates, these methods will have to be defined in the header file (seems to make sense; without a concrete iterator type, the compiler doesn't know how to generate object code for the obvious implementation of this method).


  1. 这是否是一个问题?我的直觉反应不是去在头文件中插入巨大的代码块,但我的C + +有点生锈与没有太多的STL经验外面的玩具示例,我不知道什么可接受的C ++编码实践。

  1. Is this actually a problem? My gut reaction is not to go around sticking huge chunks of code in header files, but my C++ is a little rusty with not much STL experience outside toy examples, and I'm not sure what "acceptable" C++ coding practice is on this.

有没有更好的方法来公开此功能,同时保留类似STL的通用
编程风格?一种方法是这样:

Is there a better way to expose this functionality while retaining an STL-like generic programming flavour? One way would be something like this:

(结束列表)

class RestrictedVector<T>
{
public:
  RestrictedVector(std::vector<T> wrapped)
    : wrapped(wrapped) {}

  template <class InIter>
  void Add(InIter first, InIter last)
  {
    std::copy(first, last, std::back_insert_iterator(wrapped));
  }

private:
  std::vector<T> wrapped;
};

,然后在Mesh上公开这些实例,但是这开始有点过度:非常感谢任何建议!

and then expose instances of these on Mesh instead, but that's starting to reek a little of overengineering :P Any advice is greatly appreciated!

推荐答案

这些方法必须在头文件中定义

它们必须在 头文件中定义,因此如果它们被使用,那么它们在翻译单元函数实例化。如果你担心头文件中的模板太多,减慢使用Mesh的翻译单元的编译,但实际上不使用该模板函数,那么你可以将实现移动到一个单独的头文件中。

They have to be defined in a header file, so that if they're used then they're available in the translation unit where the template function is instantiated. If you're worried about too many templates in header files, slowing down compilation of translation units which use Mesh but don't actually use that template function, then you could move the implementation into a separate header file. Makes life slightly more complicated for clients, deciding whether to include the "full fat" class header or not, but it's not actually difficult.

或者,对于这个特定的例子,你可以使用full fat可以为Mesh定义一个输出迭代器,用于追加Normals。然后客户端用他们的任意迭代器可以做:

Alternatively, for this particular example you could define an output iterator for Mesh, which appends Normals. Then clients with their arbitrary iterators can do:

std::copy(first, last, mymesh.normalAdder());

模板代码中需要的唯一标题是< algorithm& ,他们很可能已经。

The only header they need with template code in it is <algorithm>, which they quite possibly have already.

要自己做, normalAdder()返回的对象需要重载 operator ++ () operator *(),它本身需要返回一个代理对象(通常 * this ),它实现 operator =(const& Vector3)。这附加到法线的向量。

To do it yourself, the object returned by normalAdder() needs to overload operator++() and operator*(), which itself needs to return a proxy object (usually *this) which implements operator=(const &Vector3). That appends to the vector of normals. But all that is non-template code, and can be implemented in your .cpp file.

在这个例子中, normalAdder()

Again in this example, normalAdder() could just return std::back_inserter(this.normals);, a template from <iterator>.

至于你是否需要担心它 - 我认为当编译时间向天空,它更经常是由于不必要的依赖,而不是由于小位标头中的模板代码。一些大型项目似乎需要采取激烈的措施,但是我个人没有处理过大约100个文件。

As to whether you need to worry about it - I think when compilation times go skyward, it's more frequently due to unnecessary dependencies rather than due to small bits of template code in headers. Some large projects seem to need drastic measures, but personally I haven't worked with anything over about 100 files or so.

这篇关于STL化C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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