将多个大小的数组传递给虚方法而不使用模板(Passing multiple sized arrays to virtual method without using templates)

编程入门 行业动态 更新时间:2024-10-25 20:28:43
多个大小的数组传递给虚方法而不使用模板(Passing multiple sized arrays to virtual method without using templates)

所以我有一个纯粹的虚拟课,需要保持这种状态。 在这个类中,我有或者技术上需要一个接受模板化参数的方法。 以下是参数的对象类型:

template <int LENGTH> struct MyStruct { int arr[LENGTH]; };

这是我的方法:

template <int LENGTH> virtual bool send_struct(const MyStruct<LENGTH>& mystruct) = 0;

但是,显然我不能将模板与虚拟类一起使用,另一种方法是将模板添加到类中,这也是我无法实现的目的。 在保持我的约束的同时,有替代方案吗? 我需要在我的纯虚拟类中将不同大小的数组传递给此方法,但我不能将该类化解。

So I have a pure virtual class that needs to stay that way. In this class I have or technically need a method that takes in a templatized parameter. Here is the object type for the parameter:

template <int LENGTH> struct MyStruct { int arr[LENGTH]; };

And here is my method:

template <int LENGTH> virtual bool send_struct(const MyStruct<LENGTH>& mystruct) = 0;

However, obviously I cannot use a template with a virtual class, the alternative being adding the template to the class, which I also cannot do for my purpose. Is there an alternative to this while keeping my constraints? I need to pass array of different sizes to this method inside my pure virtual class, but I cannot templatize the class.

最满意答案

你知道,你不能写这个:

template <int LENGTH> virtual bool send_struct(const MyStruct<LENGTH>& mystruct) = 0;

没有办法通过编译时 LENGTH 。 但是你可以通过类型擦除容器来传递运行时长度:

virtual bool send_struct(gsl::span<int const> ) = 0;

gsl::span<T>是T s的非拥有,连续容器。 它是对数组或vector或其他任何东西的视图,也可以是MyStruct<N> 。 这种类型不能直接从你的struct构造,但是很容易编写一个版本,或者为你的( .data()和.size() )添加必要的成员以使其工作。

一个非常简单的实现就是:

class my_span { private: int const* begin_; int const* end_; public: template <int L> my_span(MyStruct<L> const& ms) : begin_(ms.arr) , end_(ms.arr + L) { } int const* begin() const { return begin_; } int const* end() const { return end_; } int const* data() const { return begin_; } size_t size() const { return end_ - begin_; } };

现在,对于一些不可修改的int容器,你有一个易于覆盖的virtual函数。

You can't write this, as you know:

template <int LENGTH> virtual bool send_struct(const MyStruct<LENGTH>& mystruct) = 0;

There is no way to pass through a compile-time LENGTH. But you can pass through a run-time length by type-erasing your container:

virtual bool send_struct(gsl::span<int const> ) = 0;

gsl::span<T> is a non-owning, contiguous container of Ts. It's a view onto an array or a vector or whatever else, which can be a MyStruct<N> too. This type isn't directly constructible from your struct, but it's easy to write a version that is, or to add the necessary members to yours (.data() and .size()) to make it work.

An extremely simple implementation would just be:

class my_span { private: int const* begin_; int const* end_; public: template <int L> my_span(MyStruct<L> const& ms) : begin_(ms.arr) , end_(ms.arr + L) { } int const* begin() const { return begin_; } int const* end() const { return end_; } int const* data() const { return begin_; } size_t size() const { return end_ - begin_; } };

And now you have a easily overridable virtual function for some non-modifiable container of ints.

更多推荐

本文发布于:2023-08-07 05:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   而不   数组   大小   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!