English 中文(简体)
为何在使用算术和准入操作者时进行预测
原标题:why upcast when using arithmetic and access operators

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. 

两个领域? 是否还预示了某种惩罚? 算术经营者是否没有公开向地球领域提供:这不是公共继承的全部内容?

我没有这样做,而是从一个我正在开发的骨质图书馆中挑选。

感谢!

最佳回答

我只能猜测,但一些测量作业可能已经超出在<代码>geom Field上的文字,以便进行几何制图。

如果您有<代码>geom Field中的数据,但希望开展未规划的行动,这可能是进行预测的原因。

另一方面,如果在投稿后执行的业务中没有一个在<代码>geom Field上手写过。 那么,你就不必再 right。

And generally, upcasting doesn t introduce any performance drawbacks because usually (or even in every case, I m not sure right now ...) it is done completely at compilation time: Consider the following classes:

class A {
public:
  void f();
};

class B : public A {
public:
  void f(); // Overwrites A::f
};

当你重新制定法典时

B obj;
obj.f();

汇编者将其翻译成类似内容。

B::f(&obj);

以及

static_cast<A>(obj).f();

成果

A::f(&obj);

i. 不存在通过投放而产生的管理费。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签