Dynamic objects

This is a discussion on Dynamic objects within the C++ Programming forums, part of the General Programming Boards category; Hi, Ok, I'm trying to write a little app to be used by myself and my collegues when teaching technical ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18

    Dynamic objects

    Hi,

    Ok, I'm trying to write a little app to be used by myself and my collegues when teaching technical training courses. The purpose of the app is to auto-mark lab exercises.

    I've posted elsewhere some other questions, but this one involves the storeage of mark data.

    I have an XML file which contains the marking scheme for a particular lab. In it are specifications of what registry / file settings are needed on a remote machine in order to get a particular mark / set of marks.

    The interface is to be a notebook with a page for each student. The idea being that when a student says they have finished, the appropriate page is brought up and a "mark" button pressed. This will kick off the process of scanning the machine.

    So far so good. Now, I'm using wxWidgets, so I thought that I could use a wxArray to hold the information on the machines. Each element being an object representing the marking for hat machine.

    The question is, how do I create those objects? I can't design the object until the XML file is read in for the particular lab exercise. This will then define both the data storage requirements and also the dislay requirements for thepages in the wxNotebook.

    Can anyone offer any advice on how to deal with objects whose format you don't know until runtime?

    Many thanks,

    Paul

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Put some kind of identifier or tag in the XML that tells you what class to instantiate, which you can then do dynamically with an abstract factory.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    For the XML portion you could download TinyXML from sourceforge. Google it and you will get to the page quickly.

    TinyXML is not the most robust XML util out there but it is very simple to use and you will have your files loaded in no time flat. It also comes with some tutorials and doxygen docs which ease the pain a bit.

    If you are really adventurous you might try Xerces XML which is extremely powerful but also extremely difficult to get setup and working. It's auto generated code is some of the ugliest C++ code I've ever had to read but thankfully you don't need to edit these files.

    You must know something about the format of the XML to read it in with TinyXML and Xerces. The schema is a must have. Xerces may be what you need because it can serialize and de-serialize class objects directly to/from XML. It creates classes for you and is a great tool but the learning curve to become proficient at it is at least a couple of days if not more.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    Thanks, guys, for your responses.

    I think you've both missed the thrust of my question. I have the XML side of things sorted. What I need to know is how to structure my data once I have read it in. The XML file might look something like:-

    Code:
    <examkeyfile>
       <title>Test Exam Keyfile</title>
       <paper type="Written" totalmark="63" passmark="80" method="cumulative">
          <manualentry>Marks Awarded</manualentry>
       </paper>
       <paper type="Lab" totalmark="123" passmark="80" method="deduct">
          <manualselect value="35">Basic Functionality Test</manualselect>
       </paper>
       <paper type="troubleshooting" totalmark="3" passmark="65" method="cumulative">
          <manualselect value="1">Issue 1</manualselect>
          <manualselect value="1">Issue 2</manualselect>
          <manualselect value="1">Issue 3</manualselect>
       </paper>
    </examkeyfile>
    In this case, I have a manual entry mark for the first paper which has an 80% passmark and a maximum total of 63. Paper 2 has a maximum mark of 123, again a pass mark of 80% but in this case the student starts with 100% and looses marks if things aren't met. The only mark (so far) is a functionality test, worth 35 marks and will require just an on/off tick-box. Paper three is again just tick-boxes, 3 off. 2 out of 3 required.

    So, as I read this in, I'm goung to need to create an array of objects which contain a couple of integer properties and a few boolean. If the exam has marks for different things, then the objects created will need different properties.

    Does that make sense?

    Paul

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes. Now do what medievalelks said.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    But that's the point. As I write the application, there is an infinite number of possible classes, depending on the lab. I don't want to hard-code this into the application, as this would make the XML file pointless. The file structure essentially defines the format of the object, in terms of what properties it will need. Using my example, I might have a second lab which has 4 bools required for paper 3. A third which might have 6 in paper 2 and so on. I can't say what the object will look like up-front in order to embed a tag to tell the app which to instantiate.

    I want to design the data structure at runtime, based on the XML file.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    maybe something like this: (pardon my pseudocode)

    Code:
    class property
    {
    //etc
    };
    
    template<typename T> class labProperty : public property
    {
    //etc
    };
    
    class labExam
    {
        public:
        std::vector<property>properties;
    //etc.
    };
    or if you have a fixed number of data types, you could just have a vector for each kind in the labexam class.

    maybe i'm still missing the point...i don't see what's so tricky here...
    Last edited by m37h0d; 06-25-2008 at 10:05 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I don't see it either. Anything you can do in that XML, you can model using classes and vectors or maps thereof.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    Perhaps I haven't fully explained myself. Whilst I'm not new to programming, C++ is a new area for me, as is Windows programming, so I'm learning as I go along. As such I may well not be able to coach my questions appropriately. "Vectors or maps thereof"??? A link to a damned good howto / tutorial would be good!!!

    I guess I'm looking for how to impliment a class with methods that allow me to add properties (perhaps with some sort of internal array) but for that class and it's property set to be duplicated into an array.

    Paul

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you can't google "C++ vectors" we're in more trouble than we thought. And aren't there tutorials on the front page of this very site? (Hint: yes.)

    But anyway, vectors are arrays, except we don't need to know how many objects ahead of time (it can grow automatically as we add to it). But, like arrays, everything we add needs to be the same type (hence making a generic type of "property" or whatever).

  11. #11
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    Ok, I think I get what you guys are saying. Correct me if I'm wrong, though, wouldn't the result of this be to add the various properties (vectors or whatever) to a particular instance of a class? What I want to do is to add the properties to the class and then instantiate that class (with all of it's properties) several times into an array (once for each student).

    I can understand why people are saying it's easy if what you want to do is add properties to an instance, but to the actual class?

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's not possible. To the best of my knowledge, there isn't a single language in existence that allows modification of real classes after they have been loaded.

    Furthermore, how would you access those fields?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jan 2007
    Location
    Northamptonshire, UK
    Posts
    18
    The same way I would access them if I had added them to an instance of an object...

    Humm. I suspect I'm going to have to instanciate a class, add the properties and then come up with a copy method so I can copy that object multiple times. Messey, but doable.

    Thank you to everyone for looking at this for me and especially to those of you who have replied :-)

    Paul

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by wierdbeard65 View Post
    Humm. I suspect I'm going to have to instanciate a class, add the properties and then come up with a copy method so I can copy that object multiple times. Messey, but doable.
    Hey, were you listening?
    It's not possible to add properties after an object is instantiated.
    You could emulate such a thing with a index operator [], though, that takes a string or integer.
    Like a map.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Well, instead of adding new member variables to an instance of a class, have the object hold a collection of property objects. Then when you need to make a change, add new property objects to the object's collection.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers to dynamic objects
    By mike_g in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 01:16 PM
  2. Replies: 4
    Last Post: 11-02-2006, 10:41 AM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. dynamic array of objects
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2003, 08:35 AM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21