本文介绍了模板&amp;类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有2个成员完全相同的成员(除了 dtor / ctor之外都是静态的)。 类只在一个静态成员中有不同的实现 函数和first class还有一个成员函数。 如何用模板编写这段代码? 首先:想写点带模板的代码是否正确? 类的成员是静态的,因为它引用了设备。没有 理由是非静态的。 谢谢。 2课程如下: ------------------ class ALDevices { static std :: vector< std :: stringlstDevices; //和MOOOOOORE STATICs共享2堂课程! public: ALDevices(); // 2个类中的实现不同 static void closeDevice(); // 2个类中的不同实现 static int getTotalDevices(){return lstDevices.size(); } //和MOOOOOORE STATIC共享2堂课! static ALCcontext * getActiveContext(){return context; } // DOEN''T 存在于所有第二类中 }; ----------- ------- 类ALCaptureDevices { static std :: vector< std :: stringlstDevices; //和MOOOOOORE STATIC共享2堂课程! public: ALCaptureDevices(); // 2个类中的实现不同 static void closeDevice(); // 2个类中的不同实现 static int getTotalDevices(){return lstDevices.size(); } //和MOOOOOORE STATIC共享2堂课! }; ------------- ----- 解决方案 两个建议: 1.重新设计你的程序;-) 2.构建一个参数化的类模板通过一种类型,将所有 重复的成员放在这里。从相应类的此类模板的 实例中派生您的类。 Markus chameleon写道: 看起来像是矫枉过正。把所有常见的东西放到一个班级,然后写两个派生类来获得。 - - - Pete Roundhouse Consulting,Ltd。( www.versatilecoding.com ) 标准C ++库扩展:教程和参考的作者。 ( www.petebecker.com/tr1book ) 似乎有点矫枉过正。把所有常见的东西放到一个类中,并写两个派生类的。 这里的问题是:所有常见的东西都是静态成员。 我想要2个模板实例,所以,2个静态成员实例。 如果我从一个派生两个类,两个类共享常见的静态成员。 I have 2 classes with exactly the same members (all static exceptdtor/ctor).Classes have different implementantion in only one static memberfunction and first class has one more member function. How can I write this code with templates?First of all: Thought to write code with templates is correct? members of classes are static because refer to devices. There is noreason to be non-static. Thanks. 2 classes follow:------------------class ALDevices{static std::vector<std::stringlstDevices;// AND MOOOOOORE STATICs SHARED IN 2 CLASSES! public:ALDevices();// IMPLEMENTENT DIFFERENT IN 2 CLASSESstatic void closeDevice();// IMPLEMENTENT DIFFERENT IN 2 CLASSES static int getTotalDevices() { return lstDevices.size(); }// AND MOOOOOORE STATICs SHARED IN 2 CLASSES! static ALCcontext *getActiveContext() { return context; }// DOEN''TEXIST AT ALL IN SECOND CLASS};------------------class ALCaptureDevices{static std::vector<std::stringlstDevices;// AND MOOOOOORE STATICs SHARED IN 2 CLASSES! public:ALCaptureDevices();// IMPLEMENTENT DIFFERENT IN 2 CLASSESstatic void closeDevice();// IMPLEMENTENT DIFFERENT IN 2 CLASSES static int getTotalDevices() { return lstDevices.size(); }// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!};------------------ 解决方案 Two suggestions:1. Redesign your program ;-)2. Build a class template parameterized by a single type, put allduplicated members in here. Derive both your classes from theinstantiations of this class template for the respective class. MarkusSeems like overkill. Put all the common stuff into a class, and writetwo derived classes. -- -- PeteRoundhouse Consulting, Ltd. (www.versatilecoding.com)Author of "The Standard C++ Library Extensions: a Tutorial andReference." (www.petebecker.com/tr1book) Seems like overkill. Put all the common stuff into a class, and writetwo derived classes.The problem here is that: All common stuff is static members.I want 2 template instantiations, so, 2 static member instances. If I derive 2 classes from one, both classes share common static members. 这篇关于模板&amp;类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 23:20