what would be a reason for upcasting in C++ in the code which will be used for numerical computations over huge amounts of data (a library that I m using)?
考虑以下等级:
template<class T>
class unallocatedArray
{
public:
unallocatedArray(int size, T t)
: size_(size), t_(0)
{
}
// Copy constructor. This is the only way to
// actually allocate data: if the array is
// passed as an argument to the copy constr.
// together with the size.
// Checks. Access operators. Iterators, etc.
// Wrappers for stl sorts...
private:
int size_;
T* t_;
};
template<class T>
class myArray
: public unallocatedArray<T>
{
public:
// This type actually allocates the memory.
myArray (int size)
: unallocatedArray<T>(size)
{
// Check if size < 0..
// Allocate.
this->t_ = new T[size];
}
myArray (int size, T t)
{
this->t_ = new T[size];
for (int i = 0; i < this->size_; i ++ )
{
this->t_[i] = t;
}
}
// Some additional stuff such as bound checking and error handling.
// Append another array (resizing and memory copies), equality
// operators, stream operators, etc...
};
template<class T>
class myField
: public myArray<T>
{
public:
// Constructors call the parent ones. No special attributes added.
// Maping operations, arithmetical operators, access operator.
};
//
template<class T>
class geomField
: public myField<T>
{
// myField but mapped on some kind of geometry, like surface meshes,
// volume meshes, etc.
};
这只是一个非常简化的模式,只是说,准入经营人的定义是一切的,算术操作者被安排在我的外地班。 现在,如果需要做1 e07倍以下的话,那么把地平地推向我的外地的理由是什么:
access the element perform arithmetical expression
in the following way:
GeomField<myType> geomFieldObject;
myField<myType>& downCast = geomFieldObject;
// Do the arithmetical operations on the downCast reference.
两个领域? 是否还预示了某种惩罚? 算术经营者是否没有公开向地球领域提供:这不是公共继承的全部内容?
我没有这样做,而是从一个我正在开发的骨质图书馆中挑选。
感谢!