问题描述
我有一个模板类层次结构,
I have a template class hierarchy,
___ Class (ClassA)
|
AbstractClass_____
|___ Class (ClassB)
和ClassB,我有一个模板类型的const NullPosition,在ClassA和ClassB是不同的。在类classA和ClassB中,我必须做一些取决于NullPosition的值的操作。
in classA and ClassB, I have a const NullPosition of a templated type, which is different in ClassA and ClassB. In class classA and ClassB I have to do some operation which are dependant on the value of the NullPosition.
现在我需要做一些操作取决于NullPosition的值,但我有困难的时候,因为变量是不同的类型和值。更具体地说,NullPosition在类A中标识一个无效的数组索引,因此等于-1;
在classB中它标识一个NULL指针,因此它等于0.
Now I need to do some operations depending on the value on NullPosition, but I am having hard time since the variable are different type and values. To be more specific NullPosition in classA identifies an invalid array index, therefore equals -1;in classB it identifies a NULL pointer therefore it equals 0.
请在下面找到一个例子。
Please find below an example.
#ifndef ABSTRACTCLASS_H
#define ABSTRACTCLASS_H
template <class T, class P>
class AbstractClass
{
public:
typedef T Type;
typedef P Position;
void MethodX() const;
virtual Position Method() const = 0;
};
template <class T, class P>
void AbstractClass<T,P>::MethodX() const
{
Position p=Method();
/*
what I am trying to achieve is being able to use the constant NullPosition in abstract class.
if (p==NullPosition)
cout<<"p is equal NULLPOSITION";
else
cout<<"p is not equal NULLPOSITION";
*/
}
#endif
#ifndef CLASS_A_H
#define CLASS_A_H
#include "../AbstractClass.h"
template <class T>
class Class:public AbstractClass<T,unsigned int>
{
public:
typedef T Type;
typedef typename AbstractClass<T,unsigned int>::Position Position;
Class();
Position Method() const;
static const Position NullPosition=-1;
private:
Type* TypeArray;
unsigned int nElements;
};
template <class T>
Class<T>::Class()
{
nElements=0;
TypeArray=new Type[128];
}
template <class T>
typename Class<T>::Position Class<T>::Method() const
{
return NullPosition;
}
#endif
#ifndef CLASS_B_H
#define CLASS_B_H
#include "../AbstractClass.h"
template <class T>
struct elementNode
{
typedef T Type;
typedef elementNode* Position;
Type element;
Position nextNode;
};
template <class T>
class Class:public AbstractClass<T, typename elementNode<T>::Position>
{
public:
typedef T Type;
typedef typename AbstractClass<T, typename elementNode<T>::Position>::Position Position;
Class();
Position Method() const;
static const Position NullPosition;
private:
Position root;
Position lastElement;
};
template <class T>
const typename Class<T>::Position Class<T>::NullPosition=0;
template <class T>
Class<T>::Class()
{
lastElement=root=NullPosition;
}
template <class T>
typename Class<T>::Position Class<T>::Method() const
{
return NullPosition;
}
#endif
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Class/ClassA/Class.h"
int main(int argc, char *argv[])
{
Class<int> classA;
classA.MethodX();
system("PAUSE");
return EXIT_SUCCESS;
}
请不要让ClassA和ClassB共享相同的名称Class,可以通过改变包含路径 - #includeClass / ClassA / Class.hfor classA和#includeClass / ClassB / Class.hfor classB来在我的代码中使用它们。
Please not that ClassA and ClassB share the same name Class, so that I a can use them interchangeble in my code by changing just the include path - #include "Class/ClassA/Class.h" for classA and #include "Class/ClassB/Class.h" for classB.
推荐答案
我没有看到问题。您有如此多的选择:
I don't see the problem. You have so many choices:
- 将NullPosition作为非类型模板参数传递给基类。
- 创建一个traits类,确定某种类型的空位置。
- 添加一个抽象函数到子类所覆盖的基类以返回null位置。
- 添加抽象函数到基类,子类重写以确定位置是否为NullPosition。
- Pass NullPosition as a non-type template argument along to the base class.
- Create a traits class that determines the null position for some type. Specialize it before defining the subclasses.
- Add an abstract function to the base class that the subclasses override to return the null position.
- Add an abstract function to the base class that the subclasses override to determine whether a position is the NullPosition.
这篇关于C ++虚拟变量在继承类层次结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!