Tuesday, 27 August 2013

C++ virtual variable in inheritance class hierarchy

C++ virtual variable in inheritance class hierarchy

I have a template class hierarchy,
___ Class (ClassA)
|
AbstractClass_____
|___ Class (ClassB)
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.
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";
*/
}

No comments:

Post a Comment