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

  • Home
  • SEARCH
  • 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 7548831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:47:53+00:00 2026-05-30T09:47:53+00:00

I have read a bit about casting in C++. Coming from a C background,

  • 0

I have read a bit about casting in C++. Coming from a C background, using normal (type) casting is common for things like void * but for C++, there are dynamic_cast, reinterpret_cast, static_cast, etc.

The problem/issue/question is about which of the above casts should be used when a conversion between a base pointer and a derived pointer.

Our data storage stores a pointer to a base class (B). The functions allocate the derived pointers (D).

The code example is as follows:

class B
{ int _some_data; }
class D : public B
{ int _some_more_data; }

The code then looks something like:

D *obj = new D;
obj->_some_data = 1;
obj->_some_more_data = 2;
<store obj>

Then later when we access the data:

B *objB = <get out data>
if (objB->_some_data == 1)
{ D *objD = (D *) objB; <do some processing> }

Now the cast I am concerned about is D *objD = (D *) objB.

Which cast should we be using?

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-30T09:47:54+00:00Added an answer on May 30, 2026 at 9:47 am

    For related types that you know about but the compiler don’t, use a static_cast.

    But in your case you should not cast at all.

    You write that

    Our data storage stores a pointer to a base class (B). The functions allocate the derived pointers (D).

    That is to throw away information, which is not a good idea. Someone realized that it’s not a good idea, that in fact it could not work, and therefore tried to keep that type information dynamically in the value of B::_some_data. The total effect: to throw away the C++ support for handling that type information, and substituting a very fragile and dirty homegrown solution.

    In order to leverage the C++ support, make B a polymorphic class, i.e. with at least one virtual member:

    struct B { virtual ~B() {} };
    

    I removed the data member _some_data since apparently its only purpose was to keep track of the dynamic type, and now the C++ support does that, in practice via a so called “vtable pointer” in the object. The total object size is probably the same. The bug attraction and sheer ugliness is, however, reduced by some orders of magnitude. 😉

    Then,

    struct D
        : B
    {
        int some_more_data_;
        D( int v ): some_more_data_( v ) {}
    };
    

    And then, with polymorphic classes, your processing code can use a safe dynamic_cast, as follows:

    B* const objB = getOutData();
    if( D* const objD = dynamic_cast<D*>( objB ) )
    {
        // do some processing using objD
    }
    

    Now this manual dynamic type checking is still very dirty and reflects a non-OO system architecture. With object oriententation, instead of operations checking what kind of data they have been given, the data effectively contains pointers to appropriate specialized operations. But I think it might be best to take one step at a time, and as a first step the above: getting rid of the fragile bug-attracting homegrown dynamic type checking scheme, and using relatively clean super-duper fresh nice-smelling good looking etc. C++ support for that. 🙂

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

Sidebar

Related Questions

I have read a bit about ADO.NET Entity Framework, but there is some things
I have read a bit about lazy loading in c# and this might seem
I have read quite a bit about unobtrusive JS and how to generate it
I have read a bit about Design-Time Attributes for Components . There I found
I have read about this and people ask this a bit too often. lets
I have only read a bit about IIS Express, and am in the process
I'm new to rsync and have read a bit about excluding files and directories
I have read quite a bit about the ReadOnly databases. Therefore I was wondering
I want to come up with a language syntax. I have read a bit
I've read around quite a bit but haven't found a definitive answer. I have

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.