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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:37:20+00:00 2026-05-11T01:37:20+00:00

I have an application that has several objects (about 50 so far, but growing).

  • 0

I have an application that has several objects (about 50 so far, but growing). There is only one instance of each of these objects in the app and these instances get shared among components.

What I’ve done is derive all of the objects from a base BrokeredObject class:

class BrokeredObject {   virtual int GetInterfaceId() = 0; }; 

And each object type returns a unique ID. These IDs are maintained in a header file.

I then have an ObjectBroker ‘factory’. When someone needs an object, then call GetObjectByID(). The boker looks in an STL list to see if the object already exists, if it does, it returns it. If not, it creates it, puts it in the list and returns it. All well and good.

BrokeredObject *GetObjectByID(int id) {   BrokeredObject *pObject;   ObjectMap::iterator = m_objectList.find(id);   // etc.   if(found) return pObject;    // not found, so create   switch(id)   {     case 0: pObject = new TypeA; break;     case 1: pObject = new TypeB; break;     // etc.     // I loathe this list   }   // add it to the list   return pObject; } 

What I find painful is maintaining this list of IDs and having to have each class implement it. I have at least made my consumer’s lives slightly easier by having each type hold info about it’s own ID like this:

class TypeA : public BrokeredObject {   static int get_InterfaceID() { return IID_TYPEA; }   int GetInterfaceID() { return get_InterfaceID(); } }; 

So I can get an object like this:

GetObjectByID(TypeA::get_InterfaceID()); 

Intead of having to actually know what the ID mapping is but I still am not thrilled with the maintenance and the potential for errors. It seems that if I know the type, why should I also have to know the ID?

What I long for is something like this in C#:

BrokeredObject GetOrCreateObject<T>() where T : BrokeredObject {   return new T(); } 

Where the ObjectBroker would create the object based on the type passed in.

Has C# spoiled me and it’s just a fact of life that C++ can’t do this or is there a way to achieve this that I’m not seeing?

  • 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-11T01:37:20+00:00Added an answer on May 11, 2026 at 1:37 am

    My use-case tended to get a little more complex – I needed the ability to do a little bit of object initialization and I needed to be able to load objects from different DLLs based on configuration (e.g. simulated versus actual for hardware). It started looking like COM and ATL was where I was headed, but I didn’t want to add the weight of COM to the OS (this is being done in CE).

    What I ended up going with was template-based (thanks litb for putting me on track) and looks like this:

    class INewTransModule {   public:     virtual bool Init() { return true; }     virtual bool Shutdown() { return true; } };  template <typename T> struct BrokeredObject { public:     inline static T* GetInstance()   {     static T t;     return &t;   } };  template <>  struct BrokeredObject<INewTransModule> { public:     inline static INewTransModule* GetInstance()   {     static INewTransModule t;     // do stuff after creation     ASSERT(t.Init());     return &t;   } };  class OBJECTBROKER_API ObjectBroker {   public:      // these calls do configuration-based creations     static ITraceTool  *GetTraceTool();     static IEeprom     *GetEeprom();     // etc }; 

    Then to ensure that the objects (since they’re templated) actually get compiled I added definitions like these:

    class EepromImpl: public BrokeredObject<EepromImpl>, public CEeprom { };  class SimEepromImpl: public BrokeredObject<SimEepromImpl>, public CSimEeprom { }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 111k
  • 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 The Object.wait() Object.notify()/Object.notifyAll(). Or Condition.await() and Condition.signal()/Condition.signalAll() for Java 5+.… May 11, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer I found a solution here http://www.nabble.com/Populating-form-fields-with-Unicode-data-td21610346.html. This solution involves embedding… May 11, 2026 at 9:40 pm
  • Editorial Team
    Editorial Team added an answer The parameterless GetFields() returns public fields. If you want non-public… May 11, 2026 at 9:40 pm

Related Questions

I have an open source Java application that uses Hibernate and HSQLDB for persistence.
I'm currently working on a ray-tracer in C# as a hobby project. I'm trying
I am working on a web application (ASP.NET) game that would consist of a
I know this question has been asked a bit before. But looking around I

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.