“运行时模板”(“run time templates”)
我很确定答案是“你不能使用模板,你必须使用虚函数(动态多态)”,但似乎我必须复制很多代码,如果我去那条路线。 这是设置:
我目前有两个类,ColorImageSegmentation和GrayscaleImageSegmentation。 它们基本上是相同的,但有三个不同 - 它们在不同的类型(ColorImage和GrayscaleImage)上运行 - 一个参数,直方图的维度(3对1)是不同的 - PixelDifference函数根据图像类型而不同
如果我创建一个类
template <TImageType> class ImageSegmentation { };
我会很好。 但是,我希望将此对象作为另一个类的成员:
class MyMainClass { ImageSegmentation MyImageSegmentation; };
但是用户需要确定
MyImageSegmentation
的类型(如果用户打开灰度图像,我想实例化MyImageSegmentation<GrayScaleType>
。同样对于彩色图像,MyImageSegmentation<ColorType>
。)使用派生类,我可以存储指针,然后执行:
class MyMainClass { ImageSegmentation* MyImageSegmentation; }; ... user does something... MyImageSegmentation = new ColorImageSegmentation;
但是我怎么用模板做这样的事呢? 问题是我有很多:
typedef TImageType::HistogramType HistogramType; typedef TImageType::PixelType PixelType;
发生了一些事情,所以我不知道如何将它们转换为动态多态模型而不重复一大堆代码。
对不起,漫无目的......有人对我有什么建议吗?
谢谢,
大卫
I'm pretty sure the answer is "you can't use templates, you have to use virtual functions (dynamic polymorphism)", but it seems like I'd have to duplicate a lot of code if I went that route. Here is the setup:
I currently have two classes, ColorImageSegmentation and GrayscaleImageSegmentation. They do essentially the same thing, but there are three differences - they operate on different types (ColorImage and GrayscaleImage) - a parameter, the dimensionality of the histogram (3 vs 1) is different - The PixelDifference function is different based on the image type
If I create a class
template <TImageType> class ImageSegmentation { };
I would be in good shape. However, I want to have this object as a member of another class:
class MyMainClass { ImageSegmentation MyImageSegmentation; };
But the user needs to determine the type of
MyImageSegmentation
(if the user opens a grayscale image, I want to instantiateMyImageSegmentation<GrayScaleType>
. Likewise for a color image,MyImageSegmentation<ColorType>
.)With derived classes, I could store a pointer and then do:
class MyMainClass { ImageSegmentation* MyImageSegmentation; }; ... user does something... MyImageSegmentation = new ColorImageSegmentation;
but how would I do something like this with templates? The problem is I have a lot of:
typedef TImageType::HistogramType HistogramType; typedef TImageType::PixelType PixelType;
sort of things going on, so I don't know how I would convert them to the dynamic polymorphic model without duplicating a whole bunch of code.
Sorry for the rambling... does anyone have any suggestions for me?
Thanks,
David
原文:https://stackoverflow.com/questions/4233300
最满意答案
这是一个很好的列表和讨论:
Here is a good list and discussion:
相关问答
更多-
如何从CSS访问样式(how to access the styles from CSS)[2023-12-24]
var width = $('.iv_menu_thumbnail').width(); console.log(width); 如果这是您要求的,这将获得元素的宽度。 就我而言,你无法从css声明中获得非数值。 但是您可以使用jQuery通过jQuery设置自己的值 .css() 所以如果你想设置一个新的css值,它会是这样的。 (或覆盖它) $(someelement).css('float', 'left'); var width = $('.iv_menu_thumbnail').width( ... -
您可以混合内部和外部样式。 内部,外部和内联样式都是级联的,内联样式优先于内部样式,内部样式本身优先于外部样式。 如果要根据用户权限动态更改样式,为什么不在body标签中添加相关的类和id,例如 然后使用CSS将角色和组分开,例如 #admin{ background-color: rgb(255,155,105); } .group-1{ font-family: sans-serif; } .group-2{ ...
-
我的CSS样式消失了吗?(My CSS Styles are Disappearing?)[2021-08-25]
在断点外移动按钮 Move the button outside the breakpoint -
CSS的完整概念是C = Cascading。 您的要求是从等式中删除级联。 PrimeFaces CSS不应该与PrimeFaces的任何其他CSS规则冲突,CSS用于组件。 你更希望引导程序采用“Master”CSS指南,在这种情况下你应该使用facet进行排序,PrimeFaces包含first方面的资源:
-
通常,是的,所有的CSS都应该包含在样式表中。 有几个例子可以让CSS显示为inline(display:none,email css等)。 有很多CSS方法可以帮助您发现最佳实践,我使用这种方法: http : //smacss.com/ 我更喜欢使用CSS样式表中的类,并为jQuery使用预留ID。 Generally, yes all CSS should be contained in a style sheet. There are a few instances where it is ok f ...
-
内联样式与CSS中的样式(Inline styles vs styles in CSS)[2023-05-04]
使用内联CSS: 对页面中的每个元素重复相同的规则。 更多的代码和更大的文件大小传输到客户端。 更难以维护,假设您想将宽度更改为200px,则需要浏览所有页面并逐个进行编辑。 一致: 外部或将css类放在头部[嵌入式样式]: ... -
反应不呈现CSS样式(React not rendering css styles)[2023-09-25]
对于许多反应组件,相关的CSS包含在npm模块中,但不能通过import加载,因为它们不是JS。 通常这些样式必须单独加载。 文档说: 要包含默认样式,请将以下import语句添加到您的手写笔文件中:@import'node_modules / react-selectize / themes / index.css' 你有包括吗? 如果你不使用Stylus,你可以抓住CSS并将其粘贴到主CSS文件中。 For many react components, relevant CSS is included ... -
不同的CSS样式(Different CSS Styles)[2022-11-20]
这是一个很好的列表和讨论: http://css-tricks.com/different-ways-to-format-css/ Here is a good list and discussion: http://css-tricks.com/different-ways-to-format-css/ -
目前在CSS中是不可能的。 但最终可能有一个属性可以做到这一点: all 它可能需要三个值: initial | inherited | unset 取自级联和继承模块 : “例如,如果作者在元素上指定all:initial,它将阻止所有继承并重置所有属性,就好像级联的作者,用户或用户代理级别中没有出现规则一样。” 根据截至2017年6月的MDN文档 ,目前all 文档 all受Chrome,Firefox / Mobile和Opera的支持。 Safari仅支持CSS4值revert ,其他浏览器不支持。 ...
-
CSS表格样式(CSS table styles)[2022-09-29]
我认为你只需要使表默认的CellSpacing和CellPadding 0(假设帖子中的顶部图片是所需的结果)