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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:38:26+00:00 2026-06-15T05:38:26+00:00

I have a Cheese class. In my program, I deal a lot with collection

  • 0

I have a Cheese class. In my program, I deal a lot with collection of cheeses, mostly vector<Cheese> objects.

I want to be able to eat() a cheese collection, something like this:

vector<Cheese> cheeses;
//cheeses = ...
cheeses.eat();

How to do this? How do I add a new member function to the vector<Cheese> class? Should I just subclass the vector<Cheese> class, name the subclass CheeseCollection and add the member function there, or are there any better ways?

Coming from Objective-C, I’m used to categories, which allowed me to add functions (“methods”) to classes. Is something like that available in C++, or is it considered more natural to subclass like crazy in C++?

  • 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-15T05:38:27+00:00Added an answer on June 15, 2026 at 5:38 am

    In C++ you simply wouldn’t use a member function for this – use a free function:

    void eat(std::vector<Cheese> const& cheeses) {
        // …
    }
    

    This is a close equivalent to those Obj-C categories even though the syntax differs (and you’re not using member access).

    The standard library container classes weren’t designed to be subclassable so that approach will fail. What you could do is use composition instead of inheritance – i.e. have a CheeseCollection class which contains a vector of cheeses as a member. This may have some advantages, depending on your overall design. However, in general the above is the most C++ic solution.

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

Sidebar

Related Questions

Suppose I have some objects, and I want the user to be able to
Say I have a MongoDB collection with documents like— { name: Hawaiian, toppings: ['cheese',
I want to: Describe a fact about a subset of a class of objects.
I have a list of cheese objects. Cheese has a method that does a
Suppose I'd like to upload some eggs on the Cheese Shop. Do I have
have written this little class, which generates a UUID every time an object of
I have an ArrayList of Products, that i want to add to My map
I have one UserControl nested inside the other UserControl, something like that: <uc:MyControl1> <uc:MyControl2
I have an object that I want to be modified when I call the
I have a string (url path) such as /recipes/cheese/cheese-meat-loaf and I have a giant

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.