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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:35+00:00 2026-05-22T15:28:35+00:00

I am running a simulation in which I have objects of a class which

  • 0

I am running a simulation in which I have objects of a class which use different models. These models are randomly selected for some objects of the class and specifically decided for some objects too. These objects communicate with each other for which I am using structures (aka struct) in C++ which has some

  1. standard variables and
  2. some additional variables which depends on models which the objects communicating with each other have.

So, how can I do this?

Thanks 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-22T15:28:36+00:00Added an answer on May 22, 2026 at 3:28 pm

    All instances of a structure or class have the same structure. Luckily, there are some tricks that can be used to ‘simulate’ what you try to do.

    The first trick (which can also be used in C), is to use a union, e.g.:

    struct MyStruct
       {
       int field1;
       char field2;
       int type;
       union
          {
          int field3a;
          char field3b;
          double field3c;
          } field3;
       };
    

    In a union, all members take up the same space in memory. As a programmer you have to be careful. You can only get out of the union what you put in. If you initialize one member of a union, but you read another member, you will probable get garbage (unless you want to do some low-level hacks, but don’t do this unless you are very experienced).

    Unions often come together with another field (outside the union) that indicates which member is actually used in the union. You could consider this your ‘condition’.

    A second trick is use the ‘state’ pattern (see http://en.wikipedia.org/wiki/State_pattern). From the outside world, the context class looks always the same, but internally, the different states can contain different kinds of information.

    A somewhat simplified approach for state is to use simple inheritance, and to use dynamic casts. Depending on your ‘condition’, use a different subclass, and perform a dynamic cast to get the specific information.

    E.g., suppose that we have a Country class. Some countries have a president, others have a king, others have an emperor. You could something like this:

    class Country
       {
       ...
       };
    
    class Republic : public Country
       {
       public:
          const string &getPresident() const;
          const string &getVicePresident() const;
       };
    
    class Monarchy : public Country
       {
       public:
          const string &getKing() const;
          const string &getQueen() const;
       };
    

    In your application you could work with pointers to Country, and do a dynamic cast to Republic or Monarchy where the president or king is needed.

    This example can be easily transformed into one using the ‘state’ pattern, but I leave this as an exercise for you.

    Personally, I would go for the state pattern. I’m not a big fan of dynamic casts and they always seem to be kind-of-hack for me.

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

Sidebar

Related Questions

(Note: The project is in Python.) I'm running a simulation in which I have
I have to run a simulation with several postgresql databases spread on different machines
I am running simulation code that is largely bound by CPU speed. I am
I'm running a long simulation in MATLAB that I've realized I need to stop
I am running a script that animates a plot (simulation of a water flow).
I have an iphone simulator running on my Mac. I have a .ipa file,
I have an iPhone app running in the simulator that won't quit. I also
I have situation, where running a query that filters by an indexed column in
I have a situation where user requests to do a long running process. I
I have a situation where I might have multiple instances of a program running

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.