纯虚拟模板......

编程入门 行业动态 更新时间:2024-10-27 20:36:15
本文介绍了纯虚拟模板......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我不认为我能够正确描述我的问题,所以 而不是我会只是给出一个我正在努力的伪C ++代码。 基本上我正在寻找一个''纯虚拟模板''函数,我可以在b $ b中声明基类(*)。 感谢您的建议, -Mathieu (*) struct Base { }; struct A:public Base { 模板< typename Tvoid foo(){} }; struct B:public Base { 模板< typename Tvoid foo(){} }; int main(int argc,char * argv []) { 基础*基础; if(argc) { base = new A; } else { base = new B; b $ b} base-> foo< int>(); 返回0; }

解决方案

10月26日下午2:19,mathieu< mathieu.malate ... @ gmailwrote:

你好, 我不认为我能够正确描述我的问题,所以 相反,我只会给出一个我正在努力的伪C ++代码。 基本上我正在寻找一个''纯虚拟模板''函数,我将b / b 能够在基类(*)中声明。

嗯...这里没有太多满足感是我的临时解决方案。 希望你们这些人会建议一些不涉及制作 基类是多态的... #include< iostream> struct Base { 模板< typename Tvoid foo()const; int base; virtual void bla(){} }; struct A:public Base { template< typename Tvoid foo()const {std :: cout<< " A" << std :: endl; } 加倍; }; 结构B:公共基地 { template< typename Tvoid foo()const {std :: cout<< " B" << std :: endl; } 浮动b; }; 模板< typename T> void Base :: foo()const { const A * a = dynamic_cast< const A *>(this); const B * b = dynamic_cast< const B *>(this); if(a)a-> foo< T>(); else if(b)b - > foo< T>(); } int main(int argc,char * argv []) { 基数*基数; if(argc 1) { base = new A; } 其他 { base = new B; } base-> foo< int>(); 返回0; }

mathieu写道:

你好, 我不认为我会能够正确描述我的问题,所以 而不是我只会给出一个我正在努力的伪C ++代码。 基本上我在寻找一个纯粹的虚拟技术mplate''函数,我可以在基类(*)中声明。

由于大多数编译器通过虚拟方法表实现虚拟方法, 不允许声明模板成员虚拟(抱歉我不能引用C ++标准的 段落)。如果不是这样,VMT表的大小可能不会被编译器确定(它无法预测 的实例化数量可能需要模板成员以下代码)。因此模板和 虚拟方法不能一起使用。

(*) struct Base { }; struct A:public Base { template< typename Tvoid foo(){} }; struct B:public Base { 模板< typename Tvoid foo(){} }; int main(int argc,char * argv []) { 基础*基数; if(argc) { base = new A; } else { base = new B; } base-> foo< int>(); 返回0; } 我认为你混淆了两个或多或少正交的C ++编程概念 语言:模板用于重用编译时已知逻辑的代码 时间(这意味着你确切知道你的鳕鱼e将在编译 代码时执行,而虚拟方法用于重用代码,其语义将在运行时确定为。如果您在编译时知道每个类A和B将使用模板参数int进行foo实现,那么您可以执行以下操作: struct Base { 虚拟空虚foo_int()= 0; }; struct [A同样B]:公共基地 { 模板< typename Tvoid foo(){} 虚拟空虚foo_int() { foo< int(); } }; int main(int argc,char * argv []) { : base-> foo_int(); 返回0; } 请注意,涉及两种不同的解析机制:模板 编程使用应该使用模板函数实例化确定 (推导出适当的模板参数),而虚拟方法采用涉及VMT的解析机制:此表决定于 运行时哪种方法应该被调用。你正试图让两个机制一起工作 。由于上述原因,这不会起作用。 问候, Stuart

10月26日下午1:49,mathieu< mathieu.malate ... @ gmailwrote:

10月26日下午2:19,mathieu< ; mathieu.malate ... @ gmailwrote:

你好,

我不认为我能够正确描述我的问题,所以 而不是我只会给出一个我正在努力的伪C ++代码。 基本上我正在寻找一个''纯虚拟模板''函数,我可以在基类(*)中声明。

哼哼...这里没有太多的满足感是我的临时解决方案。 希望你们这些人会建议不涉及制作 基类是多态的...

我不得不说,我真的没有看到问题或者你希望什么 实现。我怀疑你想要在不知道子类型的情况下动态调用 子成员函数。这就是 虚拟功能的用途。您可能还会看一些设计 模式,也许命令或访问者模式会有所帮助。但是,正如我所说的那样,我不会真正得到你在这里的东西。也许多一点 需要澄清。

Hi there, I don''t think I''ll be able to describe my issue correctly, so instead I''ll just give a pseudo C++ code I am struggling with. Basically I am looking for a ''pure virtual template'' function that I would be able to declare in the base class (*). Thanks for suggestions, -Mathieu (*) struct Base { }; struct A : public Base { template <typename Tvoid foo() {} }; struct B : public Base { template <typename Tvoid foo() {} }; int main(int argc, char *argv[]) { Base * base; if( argc ) { base = new A; } else { base = new B; } base->foo<int>(); return 0; }

解决方案

On Oct 26, 2:19 pm, mathieu <mathieu.malate...@gmailwrote:

Hi there, I don''t think I''ll be able to describe my issue correctly, so instead I''ll just give a pseudo C++ code I am struggling with. Basically I am looking for a ''pure virtual template'' function that I would be able to declare in the base class (*).

Hum...without much satisfaction here is my temporary solution. Hopefully you guys will suggest something not involving making the base class be polymorphic... #include <iostream> struct Base { template <typename Tvoid foo() const; int base; virtual void bla() {} }; struct A : public Base { template <typename Tvoid foo() const { std::cout << "A" << std::endl; } double a; }; struct B : public Base { template <typename Tvoid foo() const { std::cout << "B" << std::endl; } float b; }; template <typename T> void Base::foo() const { const A * a = dynamic_cast<const A*>(this); const B * b = dynamic_cast<const B*>(this); if( a ) a->foo<T>(); else if( b ) b->foo<T>(); } int main(int argc, char *argv[]) { Base * base; if( argc 1 ) { base = new A; } else { base = new B; } base->foo<int>(); return 0; }

mathieu wrote:

Hi there, I don''t think I''ll be able to describe my issue correctly, so instead I''ll just give a pseudo C++ code I am struggling with. Basically I am looking for a ''pure virtual template'' function that I would be able to declare in the base class (*).

As most compilers implement virtual method by means of virtual method tables, declaring a template member virtual is not allowed (sorry that I cannot cite the paragraph of the C++ standard). Were it not so, the size of the VMT table could not be determined by the compiler (it cannot predict how many instantiations of the template member could be needed in the following code). Thus templates and virtual methods don''t go together.

(*) struct Base { }; struct A : public Base { template <typename Tvoid foo() {} }; struct B : public Base { template <typename Tvoid foo() {} }; int main(int argc, char *argv[]) { Base * base; if( argc ) { base = new A; } else { base = new B; } base->foo<int>(); return 0; }

I think you mixed up two more or less orthogonal concepts of the C++ programming language: templates are used to reuse code where the logic is known at compile time (that means you know exactly what your code will do when you compile the code), whereas virtual methods are used to reuse code whose semantics will be determined at run-time. If you know at compile time that each class A and B will have a foo implementation with template parameter int, you may do the following: struct Base { virtual void foo_int () = 0; }; struct [A and likewise B] : public Base { template <typename Tvoid foo() {} virtual void foo_int () { foo<int(); } }; int main(int argc, char *argv[]) { : base->foo_int(); return 0; } Note that there are two different resolution mechanisms involved: template programming uses the determination with instantiation of a template function should be used (the proper template argument is deduced), whereas virtual methods employ a resolution mechanism involving VMTs: this table decides at run-time which method should be invoked. You are trying to get both mechansims working in a single step. For above mentioned reasons, this won''t work. Regards, Stuart

On Oct 26, 1:49 pm, mathieu <mathieu.malate...@gmailwrote:

On Oct 26, 2:19 pm, mathieu <mathieu.malate...@gmailwrote:

Hi there,

I don''t think I''ll be able to describe my issue correctly, so instead I''ll just give a pseudo C++ code I am struggling with. Basically I am looking for a ''pure virtual template'' function that I would be able to declare in the base class (*).

Hum...without much satisfaction here is my temporary solution. Hopefully you guys will suggest something not involving making the base class be polymorphic...

I have to say, I don''t really see the problem or what you''re hoping to achieve. I have a suspicion that you want to dynamically invoke a child member function without knowing the child type. That''s what virtual functions are for. You might also take a look at some design patterns maybe the command or visitor pattern would help. But, as I say, I don''t really get what you''re after here. Maybe a little more clarification is required.

更多推荐

纯虚拟模板......

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

发布评论

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

>www.elefans.com

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