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

  • SEARCH
  • Home
  • 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 7886045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:13:23+00:00 2026-06-03T05:13:23+00:00

Before anything, thanks for reading! I’m developing an application in C++ and I want

  • 0

Before anything, thanks for reading!

I’m developing an application in C++ and I want an advice about a design issue. Let me explain:

The main class of my application has some collections, but other classes eventually need to get a value from one of those collections. Something like this:

class MainClass { 
private:
   // Collections are internally implemented as QHash
   Collection<Type1> col1;
   Collection<Type2> col2;
};
class RosterUnit {
public:
   RosterUnit() {
      /* This method needs to get a specific value from col1 and
         initialize this class with that data */
   }
};

class ObjectAction {
public:
    virtual void doAction() = 0;
};

class Action1 : public ObjectAction {
public:
    void doAction() {
       // This needs a specific value from col2
    }
};

class Action2 : public ObjectAction {
public:
    void doAction() {
       // This needs a specific value from col1
    }
};

My first approach was passing the whole collection as parameter when needed, but it is not so good for ObjectAction subclasses, because I would have to pass the two collections and if I later create another subclass of ObjectAction and it needs to get an element from other collection (suppose col3), I would have to modify the doAction() signature of every ObjectAction subclass, and I think that is not too flexible. Also, suppose I have a Dialog and want to create a RosterUnit from there. I would have to pass the collection to the dialog just to create the RosterUnit.

Next I decided to use static variables in RosterUnit and ObjectAction that pointed to the collections, but I’m not very happy with that solution. I think it is not flexible enough.

I have been reading about design patterns and I first thought a Singleton with get functions could be a good choice, but after some more investigation I think it isn’t a proper design for my case. It would be easier and more or less the same if I use global variables, which don’t seem to be the right way.

So, could you give some advices, please?

Thank you very much!

  • 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-06-03T05:13:25+00:00Added an answer on June 3, 2026 at 5:13 am

    As mentioned previously, Iterators are good for abstracting away the details of the Collection. But going this route implies that the objects that use the Iterators will need to know about what’s inside the Collection. Meaning they will need to know how to decide which object in the Collection they need, thus increasing the coupling. (more details below in the Factory paragraph) This is something you need to consider.

    Another approach would be to create accessor methods on the MainClass that take some sort of key and return an object from the Collection (findObject(key)). Internally the MainClass methods would search through the container(s) and return the appropriate object. To use this approach, you will however need access to the MainClass, either by dependancy injection as mentioned before, or possibly making it a Singleton (not recomended in this scenario, though).

    With the info provided so far, it may even be better for your ObjectAction Factory to have a reference to the MainClass, and as a part of the ObjectAction creation logic, call the appropriate MainClass accessor and pass the result into the ObjectAction, thus decoupling the ObjectAction Objects from the MainClass.

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

Sidebar

Related Questions

I've just registered here, so before anything, thanks for reading! I'm developing an application
I want to write a custom validator which doesn't do anything before the user
Has anyone seen this before - and can anything be done about it? This
I'm participating in google code jam. Before anything I want to say that I
Before anything, I am not looking for a re-write. This was presented to me,
Before anything else, here is the simplified schema ( with dummy records ) of
Error that pops up even before anything else loads: Package Load Failure Package 'Microsoft.TeamFoundation.Client.ServicesHostPackage,
I need to convert a string to another which has removed anything before the
(Before anyone says anything Yes this was homework but i have already turned it
If in C I write: int num; Before I assign anything to num ,

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.