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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:53+00:00 2026-05-28T00:39:53+00:00

I’m a vegetarian, so suppose we have vegetables: class Vegetable {}; // base class

  • 0

I’m a vegetarian, so suppose we have vegetables:

class Vegetable {}; // base class for vegetables

class Tomato : public Vegetable {};
class Potato : public Vegetable {};
class Carrot : public Vegetable {};
class Broccoli : public Vegetable {};

And suppose we wanna make meals with them:

class Meal {}; // base class for meals

class Soup : public Meal {
    ...
    Soup(Vegetable *veg1, Vegetable *veg2) : veg1(veg1), veg2(veg2) {};
};

class Salad : public Meal {
    ...
    Salad(Vegetable *veg1, Vegetable *veg2, Vegetable *veg3) : veg1(veg1), veg2(veg2), veg3(veg3) {};
};

class VeggieBurger : public Meal {
    ...
    VeggieBurger(Vegetable *veg) : veg(veg) {};
};

Now we’d like to define different meals with different vegetable combinations in a cookbook:

std::vector<Meal *> cookbook;

cookbook.push_back(new Soup(new Tomato, new Potato));
cookbook.push_back(new Soup(new Potato, new Broccoli));
cookbook.push_back(new Salad(new Tomato, new Carrot, new Broccoli));
cookbook.push_back(new Salad(new Tomato, new Potato, new Tomato));
cookbook.push_back(new Salad(new Broccoli, new Potato, new Carrot));
cookbook.push_back(new VeggieBurger(new Potato));
// many more meals...

So we are creating many little objects on the heap that are getting composed together via constructor arguments and pushed onto a std::vector at runtime. Obviously the downside of this design is, that we have to manage the memory ourselves and delete the Vegetable objects in our meals destructor and delete our cookbook meals somewhere when it goes out of scope.

So a possible design choice would be to use smart pointers to out source the burden of doing memory management for our meals and vegetables.

But I’m wondering if it is possible to compose the cookbook at compile time, maybe with some kind of template magic? The cookbook does not necessarily have to be a std::vector but we still like to be able to iterate over it, get the Meal objects and call member functions on a composed meal. Are there better ways to do it?

  • 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-28T00:39:54+00:00Added an answer on May 28, 2026 at 12:39 am

    I’ve found that code generation is becoming an incredibly useful tool for me. Maybe write a short program, perhaps even just a script, than will generate the boilerplate code for you.

    Any time you add to your cookbook, you run the program/script, which will generate the headers for each meal and the header, maybe even some of the source, for the cookbook itself.

    How in-depth you get with your generation is up to you. You could just edit the code generator’s source to add new meals, you could do some simple text based parsing, or, if it’s really worth the time and effort to maintain, turn your code generator into an editor (with code generation as its output).

    At least one very well know AAA game engine actually generates C++ header files for any scripts that are marked to interface with native code. From there, a macro in a source file implements the boilerplate methods. The rest of the methods are implemented by the developers.

    Update:
    C++11 actually has support for variadic template arguments. I don’t have any experience with C++11, so I’m not sure if variadic template arguments would support what we’re looking at.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.