Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:44:21+00:00 2026-05-26T05:44:21+00:00

what would be a reason for upcasting in C++ in the code which will

  • 0

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)?

Consider the following hierarchy:

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.  
};

This is just a very extremely simplified model, just to state that the access operator is defined all the way up, and the arithmetic operators are placed in the myField class. Now, what would be the reason for upcasting geomField to myField, if one needs to perform say 1e07 times the following:

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. 

for two of such fields? Isn’t upcasting introducing some kind of penalty as well? Are the arithmetic operators not publicly available to the geomField: is this not the whole point of public inheritance?

I didn’t design this, it’s from an os library that I’m developing in. 🙂

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:44:22+00:00Added an answer on May 26, 2026 at 5:44 am

    I can only guess, but some of the arithmetical operations might have been overwritten in geomField in order to implement the geometric mapping.

    If you have data in an geomField but want to perform unmapped operations, that might be a reason for upcasting.

    If, on the other hand, none of the operations that are executed on the casted reference are overwritten in geomField then you’re right that casting is not necessary.

    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
    };
    

    When you’re code looks like

    B obj;
    obj.f();
    

    it is translated by the compiler to something like

    B::f(&obj);
    

    and

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

    results in

    A::f(&obj);
    

    i.e. there is no runtime overhead introduced by casting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any reason why JQuery would not work when used in an ASPX
I'm looking to create a global mouse hook that works in XP/Vista/7 which would
At work today we were trying to come up with any reason you would
If I'm running a suite of tests, is there a reason I would NOT
I'm using ILMerge in my post build step. For interoperability reason I would like
Is there any reason something like this would not work? This is the logic
Question: Is there any reason Autocomplete=off on a ASP:Textbox would not be working in
Question What would be a good (ideally, technical) reason to ever program some non-trivial
Is there a good reason why there is no Pair<L,R> in Java? What would
Is there a particular reason why a generic ICloneable<T> does not exist? It would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.