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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:44:08+00:00 2026-05-11T14:44:08+00:00

I inherited from C++ STL container and add my own methods to it. The

  • 0

I inherited from C++ STL container and add my own methods to it. The rationale was such that to the clients, it will look act a regular list, yet has application-specific methods they can readily be called.

This works fine, but I have read numerous posts about not inheriting from STL. Can someone provide a concrete advice of how I might write the code below in a better way?

class Item {   int a;   int b;   int c;    int SpecialB()   {     return a * b + c;   } };  class ItemList : public std::vector<Item> {   int MaxA()   {     if( this->empty() )       throw;      int maxA = (*this)[0].a;      for( int idx = 1; idx < this->size(); idx++ )     {       if( (*this)[idx].a > maxA )       {         maxA = (*this)[idx].a;       }     }     return maxA;   }    int SpecialB()   {     if( this->empty() )       throw;      int specialB = (*this)[0].SpecialB();      for( int idx = 1; idx < this->size(); idx++ )     {       if( (*this)[idx].SpecialB() < specialB )       {         specialB -= (*this)[idx].c;       }     }     return specialB;   }    int AvgC()   {     if( this->empty() )       throw;      int cSum = 0;     for( int idx = 0; idx < this->size(); idx++ )     {       cSum += (*this)[idx].c;     }      return cSum / this->size(); // average   } }; 

EDIT: Thanks for a bunch of thoughtful answers. I will create helper functions instead and from now on will never inherit from STL containers.

  • 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-11T14:44:09+00:00Added an answer on May 11, 2026 at 2:44 pm

    why you need extend vector in this way?

    use standard <algorithm> with your functors.
    e.g.

    std::min_element, std::max_element

    int max_a = std::max_element         (              v.begin(),              v.end(),              boost::bind(                  std::less< int >(),                 bind( &Item::a, _1 ),                  bind( &Item::a, _2 )              )         )->a; 

    std::accumulate – for calculate avarage

    const double avg_c = std::accumulate( v.begin(), v.end(), double( 0 ), boost::bind( Item::c, _1 ) ) / v.size(); // ofcourse check size before divide   

    your ItemList::SpecialB() could be rewrited as:

    int accumulate_func( int start_from, int result, const Item& item ) {    if ( item.SpecialB() < start_from )    {        result -= item.SpecialB();    }    return result; }  if ( v.empty() ) {     throw sometghing( 'empty vector' ); } const int result = std::accumulate( v.begin(), v.end(), v.front(), boost::bind( &accumulate_func, v.front(), _1, _2 ) ); 

    BTW: if you don’t need access to members, you don’t need inheritance.

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

Sidebar

Related Questions

How can I hide methods that are inherited from the Object class (such as
Do you know any controls inherited from the ItemsControl that have horizontal orientation of
I am doing a custom control (inherited from VisualBasic.PowerPacks.LineShape ), that should be painted
I'm to code a TExpandedShape class inherited from TShape. TExpandedShape must act like TShape
Can a class inherited from QThread and having run method can have other methods
I have a non-copyable (inherited from boost::noncopyable ) class that I use as a
I've a medium chunk of code containing 5 classes that they're all inherited from
I'm creating a new class that inherits queue from the STL library. The only
I have developed a server control inherited from WebControl that wraps any number of
JCheckBox it is inherited from JToggleButton , so clicking on text will have same

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.