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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:42:39+00:00 2026-05-18T07:42:39+00:00

Ok, I seem to be requiring forward casting from base to derived and I

  • 0

Ok, I seem to be requiring forward casting from base to derived and I think it might be a design problem so I will explain what I’m doing and you can tell me what I need change.
XML file defines a bunch of objects that are similar. They share the same base class. The main function is defined in the base class DoWork() and it’s virtual.

My program loads XML file and creates the derived classes and assigns them to a vector of type base class.

Everything is great the program works. I can iterate through the vector and call DoWork();

Now, I’ve added a GUI layer so you can modify the objects and write back a XML file. So, now my GUI code has access to the vector of base class pointers. But, this is no good because I need the info from the derived class so I can write out the XML file. Is the only solution to this dynamic casting? Could I have changed the design somehow? I know casting from base to derived is frowned upon.

EDIT: My GUI needs to display information that the derived class has as well. Just having a serialize class is not enough.

  • 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-18T07:42:40+00:00Added an answer on May 18, 2026 at 7:42 am

    There are two different approaches that you can take here. The first one is providing a virtual method that generates the XML corresponding to the element.

    The second approach is providing a visitor interface: basically you define a virtual method that accepts a visitor type. The visitor type will be called with the current object at each level of the hierarchy, generating a type of double dispatch mechanism. This way the virtual dispatch will resolve to the most derived object, that will call upon the visitor object to serialize itself.

    The first approach is simpler, while the second has the advantage of a looser coupling between each object in the hierarchy and the actual serialization format. That is, you can produce different visitors that can serialize to different formats without having to modify code in your current hierarchy.

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

Sidebar

Related Questions

I seem to always have problems with converting data to and from XML in
Seem to be having an issue with std::auto_ptr and assignment, such that the object
I seem to be missing something about LINQ. To me, it looks like it's
There seem to be many ways to define singletons in Python. Is there a
I seem to remember reading something about how it is bad for structs to
I seem right now to be embroiled in a debate with another programmer on
I seem to make this mistake every time I set up a new development
There seem to many ways to skin this particular cat - but which is
There seem to be two rival Eclipse plugins for integrating with Maven: m2Eclipse and
I seem to be getting a strange error when I run my tests in

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.