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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:24:29+00:00 2026-05-22T22:24:29+00:00

I don’t know how to explain this properly, someone please edit my title and

  • 0

I don’t know how to explain this properly, someone please edit my title and post as needed. I thought this could be solved with polymorphism, but I couldn’t get it to work. What I would like to have is as following.

I’m going to have different shapes and they all have different variables. For example, to describe a particular shape A I need:

Mandatory fields for every shape:

  • starting and ending point (composed
    of 3 double variables: x, y and z)

  • name (string)

A shape may or not need the following fields (maybe in the future I will need more)

  • Length, width, height (all double)
  • Angle
  • Other variables which could be double or string

The thing is, this needs to flexible, as every shape is going to be described using XML, which I then need to load into the program. For example a cube would work like this:

<?xml version="1.0" encoding="utf-8" ?> 
<structure name="Cube">
    <variables>
        <point name="start" />
        <point name="end" />
        <length name="width" min="1" max="10000" unit="mm" />
        <length name="height" min="1" max="10000" unit="mm" />
    </variables>
    <points>
        <point name="topLeft">
            <x>start.x</x>
            <y>start.y - height/2</y>
            <z>start.z</z>
        </point>
        <point name="topRight">
            <x>end.x</x>
            <y>end.y - height/2</y>
            <z>end.z</z>
        </point>
        <point name="bottomLeft">
            <x>start.x</x>
            <y>start.y + height/2</y>
            <z>start.z</z>
        </point>
        <point name="bottomRight">
            <x>end.x</x>
            <y>end.y + height/2</y>
            <z>end.z</z>
        </point>
    </points>
    <connections>
        <connection type="line">
            <point name="topLeft" />
            <point name="topRight" />
            <point name="bottomRight" />
            <point name="bottomLeft" />
            <point name="topLeft" />
        </connection>
    </connections>
</structure>

So what I thought is, I have a base class called CVariable (with 2 abstract methods, parseString and toString), which I then inherit to make CLength (new attribute double length), CPoint (new attribute double x, y and z) and CName (new attribute string name). I can then have a class called CShape which has a list of CVariable, CPoint and CName (the connections, just a name identifying the point to be connected). The problem is, I believe, if I have a list of CVariable, I can’t access the new attributes. Am I right?

Sorry if it’s not clear, I will edit to add any information needed. Thanks a lot in advance.

  • 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-22T22:24:30+00:00Added an answer on May 22, 2026 at 10:24 pm

    The scheme you’re thinking in is usually called a “variant”. CVariable is not a good name for this, it’s just to unspecific. But you’re thinking in the right direction already. By deriving from a common base class you can pack the elements in a common container. The trick to access those derived types is using the runtime type information system: There is the typeof keyword telling you the actual type of the object you’re referring to: http://msdn.microsoft.com/de-de/library/58918ffs.aspx

    A word of warning: Getting into a situation where one needs a variant is often a hint, that something in the design of the data structure may be flawed and rethinking the problem is a good idea.

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

Sidebar

Related Questions

Don't know if there is a better way to do this, so that is
Don't know what to do with this error. How to add data in SQL
Don't know if this is an eclipse specific problem but whenever I declare a
Don't think this is a repost, difficult to search for the word between because
Don't know why, but sometimes LocationManager is still working also after closing application. I
Don't know if it's I'm doing it wrong or if there's a bug (I
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't you think the TCPDF solution force you to reivent the wheel, in term
Don't ask me how but I managed to get accidentally the following remote branches
Don't worry, I'm not going to ask that question, yet again... I am wanting

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.