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 8251115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:08:04+00:00 2026-06-08T00:08:04+00:00

Some background first: I am working on some legacy code that implements UI interfaces.

  • 0

Some background first:

I am working on some legacy code that implements UI interfaces. The code is structured in a way that it has a base class (actually it’s a struct) WinBase which is then derived to all sorts of graphical objects.

WinBase:
 - Button
 - DialogWin
 - (etc...)
 - EditWin

WinBase has a member WinBase **children that is used to keep tabs on all of the children of each object. These children are created and destroyed during runtime in the standard new[]/delete[] fashion.

All methods/functions of these objects assume that they are receiving a pointer to a WinBase object, and dynamic_cast<> is used to ensure/validate that the appropriate pointers are being used.

A later change to the code added two more classes/structs (ListBox and TextBox), that are derived from EditWin which in turn is derived from WinBase.

So… I have pointers to WinBase that being casted to ListBox and TextBox. As I said, dynamic_cast<> is used throughout the code, but I am not sure how safe this is when a function like this constructor is called.

TextBox(WinBase *pw,(...));

How safe is it to cast directly from WinBase to ListBox and vice versa (for instance)?

  • 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-06-08T00:08:06+00:00Added an answer on June 8, 2026 at 12:08 am

    First of all, why would you want to cast a WinBase to a ListBox when a ListBox is given to a method expecting a WinBase? If you need a cast there, you should re-think the design, as methods expecting the WinBase should operate only on WinBase's interface.

    If, and only if, ListBox is derived from WinBase (either directly or indirectly), you can always cast from a ListBox* to a WinBase* (or reference), because a ListBox IS A WinBase.

    Casting the other way around is NOT safe, because you will never know if the WinBase* you got is actually a ListBox* or something else and that calls for trouble. You can of course use dynamic_cast<> to test if it really is a ListBox*, but any dynamic cast calls for a review of your design.

    Especially that kind of down-cast calls for trouble in a constructor, because when you are not getting what you wanted, what is your code supposed to do? Throwing exceptions, as the only way out is not such a good idea and at its best avoided at all (unless you know what you are doing).

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

Sidebar

Related Questions

First some background: I'm working on an application and I'm trying to follow MVVM
First some background, I am currently working on a relatively large Asp.Net MVC application
Okay, first I will begin with some background. I'm working on a project for
Some setup background first: I have a cronjob which runs a PHP file called
First some background, I've written an open source .NET library, named Duplicitiy (on github.com),
Hi and thanks for your attention. First some background on the question: I have
First, some background: I'm developing a web application using Python. All of my (text)
this is my first question on stack overflow. Some quick background, this is not
First some background. I'm parsing a simple file format, and wish to re-use the
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using

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.