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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:24:12+00:00 2026-05-25T17:24:12+00:00

The project I’m working on has some pretty nasty collection classes that I feel

  • 0

The project I’m working on has some pretty nasty collection classes that I feel could use a redesign. I’d really like to make a collection template class that takes model instances and provides a way to call type-specific functions of each child in the collection. For example, something like:

MyCollection<Student> BiologyStudents();
// [Fill the collection]
BiologyStudents.EnrollInClass(ClassList::Biology);
BiologyStudents.Commit();

The idea is that I could easily enroll all students in a class using my collection, then commit those changes to a database. My problem is in how to expose that EnrollInClass() function which belongs to the children Student objects? If my collection contains objects of a different type than Student, I would like those functions to be exposed from the collection. The only way I can think to do that with my semi-limited C++ knowledge would be to make a function that takes a parameter which references a function I know is in the containing child class. This wouldn’t provide compilation errors if you call the wrong function or provide the wrong parameters, so I’d like a way to utilize the compiler to provide these checks.

Is this possible? If so, how? As a warning, I’m used to generic programming in Java/C#, so my impression of C++ templates might be a bit off.

  • 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-25T17:24:13+00:00Added an answer on May 25, 2026 at 5:24 pm

    One way would be to use a method pointer:

    template <typename T>
    struct MyCollection {
      template <typename U>
      void ForEach(void (T::*func)(U),U param)
      {
        // for each item loop goes here
        (item.*func)(param);
      }
    };
    
    
    MyCollection<Student> BiologyStudents;
    // [Fill the collection]
    BiologyStudents.ForEach(&Student::EnrollInClass,ClassList::Biology);
    

    You would have to provide different versions for different numbers of parameters.

    With C++11, you can do this:

    template <typename T>
    struct MyCollection {
      void ForEach(std::function<void (T &)> func)
      {
        // for each item loop goes here
        func(item);
      }
    };
    
    
    MyCollection<Student> BiologyStudents;
    // [Fill the collection]
    BiologyStudents.ForEach([](Student &s){s.EnrollInClass(ClassList::Biology);});
    

    Which would not require making different versions of ForEach for different numbers of parameters.

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

Sidebar

Related Questions

Project#1 has some interfaces and classes that project#2 references. Now I want to use
The project I'm working on has an Oracle backend, and we use SQL Navigator
The project I am currently working on has, in the checkout, an option to
My project has some references. One of them has no path. I can't view
My project has a file foo that I've been using and checking in with
My project has a C++ library that I want to allow the user to
Every project invariably needs some type of reporting functionality. From a foreach loop in
A project I'm working on at the moment involves refactoring a C# Com Object
My project has both client and server components in the same solution file. I
The Project's Web section (under project properties in VS2008) has a list of debuggers:

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.