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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:15:47+00:00 2026-05-10T18:15:47+00:00

Is there any way to add a field to a class at runtime (

  • 0

Is there any way to add a field to a class at runtime ( a field that didn’t exist before ) ? Something like this snippet :

Myobject *ob; // create an object ob->addField('newField',44); // we add the field to the class and we assign an initial value to it printf('%d',ob->newField); // now we can access that field 

I don’t really care how it would be done , I don’t care if it’s an ugly hack or not , I would like to know if it could be done , and a small example , if possible .

Another Example: say I have an XML file describing this class :

<class name='MyClass'>    <member name='field1' />    <member name='field2' /> </class> 

and I want to ‘add’ the fields ‘field1’ and ‘field2’ to the class (assuming the class already exists) . Let’s say this is the code for the class :

class MyClass { }; 

I don’t want to create a class at runtime , I just want to add members/fields to an existing one .

Thank you !

  • 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. 2026-05-10T18:15:47+00:00Added an answer on May 10, 2026 at 6:15 pm

    Use a map and a variant.

    For example, using boost::variant. See http://www.boost.org/doc/libs/1_36_0/doc/html/variant.html

    (But of course, you can create your own, to suit the types of your XML attributes.)

    #include <map> #include <boost/variant.hpp>  typedef boost::variant< int, std::string > MyValue ; typedef std::map<std::string, MyValue> MyValueMap ; 

    By adding MyValueMap as a member of your class, you can add properties according to their names. Which means the code:

    oMyValueMap.insert(std::make_pair("newField", 44)) ; oMyValueMap.insert(std::make_pair("newField2", "Hello World")) ; std::cout << oMyValueMap["newField"] ; std::cout << oMyValueMap["newField2"] ; 

    By encapsulating it in a MyObject class, and adding the right overloaded accessors in this MyObject class, the code above becomes somewhat clearer:

    oMyObject.addField("newField", 44) ; oMyObject.addField("newField2", "Hello World") ; std::cout << oMyObject["newField"] ; std::cout << oMyObject["newField2"] ; 

    But you lose somewhat the type safety of C++ doing so. But for XML, this is unavoidable, I guess.

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

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using Views 2.x for Drupal 6.x it's simple to create… May 11, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer key: is parsed as null - %YAML 1.1 --- !!map… May 11, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer Why not create a second ServiceContract that has both Get… May 11, 2026 at 4:48 pm

Related Questions

Is there any way to add Field (or FieldInfo , maybe this is the
I'm developing an application for Windows Mobile Devices using Visual Studio .NET 2008 whose
Is there a way to ensure a particular conditional clause is added to the
We're currently using Lucene 2.1.0 for our site search and we've hit a difficult

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.