模板&类 [英] templates & classes
问题描述
我有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堂课!
};
------------- -----
嗨
chameleon写道:
我有两个类完全相同的成员(除了
dtor / ctor之外都是静态的)。
类只有一个静态不同的实现会员
功能和头等舱还有一个会员功能。
如何用模板编写这段代码?
all:想用模板编写代码是正确的吗?
类的成员是静态的,因为它引用了设备。没有
理由是非静态的。
两个建议:
1.重新设计你的程序;-)
2.构建一个参数化的类模板通过一种类型,将所有
重复的成员放在这里。从相应类的此类模板的
实例中派生您的类。
Markus
< blockquote> chameleon写道:
我有两个类完全相同的成员(除了
dtor / ctor之外都是静态的)。
类只有一个静态成员具有不同的实现
函数和第一类还有一个成员函数。
如何编写此代码使用模板?
首先:想用模板编写代码是正确的吗?
看起来像是矫枉过正。把所有常见的东西放到一个班级,然后写两个派生类来获得
。
-
- - Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )
标准C ++库扩展:教程和
参考的作者。 ( www.petebecker.com/tr1book )
O / H Pete Becker Y ??á??:
chameleon写道:
>我有2个成员完全相同的类(除了
dtor / ctor之外都是静态的)。
类只有一个静态成员
函数有不同的实现,而第一个类有一个更多成员函数。
如何用模板编写这段代码?
首先:想用模板编写代码是正确的吗?
似乎有点矫枉过正。把所有常见的东西放到一个类中,并写两个派生类的
。
这里的问题是:所有常见的东西都是静态成员。
我想要2个模板实例,所以,2个静态成员实例。
如果我从一个派生两个类,两个类共享常见的静态成员。
I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function 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 no
reason 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 CLASSES
static 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''T
EXIST 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 CLASSES
static void closeDevice(); // IMPLEMENTENT DIFFERENT IN 2 CLASSES
static int getTotalDevices() { return lstDevices.size(); }
// AND MOOOOOORE STATICs SHARED IN 2 CLASSES!
};
------------------
Hi
chameleon wrote:I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function 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 no
reason to be non-static.Two suggestions:
1. Redesign your program ;-)
2. Build a class template parameterized by a single type, put all
duplicated members in here. Derive both your classes from the
instantiations of this class template for the respective class.
Markus
chameleon wrote:I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function 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?
Seems like overkill. Put all the common stuff into a class, and write
two derived classes.
--
-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
O/H Pete Becker Y??á??:chameleon wrote:>I have 2 classes with exactly the same members (all static except
dtor/ctor).
Classes have different implementantion in only one static member
function 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?
Seems like overkill. Put all the common stuff into a class, and write
two 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.
这篇关于模板&类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!