473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add a method to an STL container?

Without modifying the STL code directly, what's the *simplest* way to
extend an STL container by adding a method to it?

e.g. I want to add method foo() to list<T>.

Can I do this without creating a list<T> subtype with a foo() method?
i.e. Can I add foo() directly to list<T>?

Joseph

Jul 23 '05 #1
6 1814
> Without modifying the STL code directly, what's the *simplest* way to
extend an STL container by adding a method to it?

e.g. I want to add method foo() to list<T>.

Can I do this without creating a list<T> subtype with a foo() method?
i.e. Can I add foo() directly to list<T>?

Joseph


namespace MyListOps
{
template <typename T>
void foo(std::list<T>& );

// ...
}

regards,
ben
Jul 23 '05 #2
Joseph Turian wrote:
Without modifying the STL code directly, what's the *simplest* way to
extend an STL container by adding a method to it?

e.g. I want to add method foo() to list<T>.

Can I do this without creating a list<T> subtype with a foo() method?
i.e. Can I add foo() directly to list<T>?

Joseph


define a new class is-a list<T> or has-a list<T>,no simpler way.
Jul 23 '05 #3
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Without modifying the STL code directly, what's the *simplest* way to
extend an STL container by adding a method to it?

e.g. I want to add method foo() to list<T>.

Can I do this without creating a list<T> subtype with a foo() method?
i.e. Can I add foo() directly to list<T>?

Joseph


It's almost certainly the wrong thing to do. Now you have a collection type
which is different from the standard type only because of some function you
wanted to throw in. If you decide later to add another function you will
have three "different" collection types! I strongly endorse Ben's solution
which is to make your convenience functions non-member functions. That
solution is extendable to as many convenience functions as you like without
ever making your own collection class.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #4
benben wrote:
namespace MyListOps
{
template <typename T>
void foo(std::list<T>& );

// ...
}

regards,
ben


Another option:

template<class Iterator>
void foo(Iterator first, Iterator last);

This is independant of the container type, just like STL algorithms
are. In fact, it can be used on a C-style array, just like STL
algorithms can be.
Take care,

John Dibling

Jul 23 '05 #5

"Joseph Turian" <tu****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Without modifying the STL code directly, what's the *simplest* way to
extend an STL container by adding a method to it?

e.g. I want to add method foo() to list<T>.

Can I do this without creating a list<T> subtype with a foo() method?
i.e. Can I add foo() directly to list<T>?

Joseph


Wrap the stl container in a class.

template< class T >
class SuperList
{
std::list<T> m_list;
public:
foo();
... // push, pop, etc
};

Jul 23 '05 #6


snnn wrote:
define a new class is-a list<T> or has-a list<T>,no simpler way.


You can't get 100% is-a:

std::auto_ptr<std::list<int> > will fail, as it tries
to call std::list::~list instead of ypur foolist::~foolist.

Regards,
Michiel Salters

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
1
by: Ryan McLean | last post by:
Hi everyone! What is happening is the method: sub_btnSubmitClicked is being executed every time any other object with a Handler is executed. I am trying not to use the withevents and handles...
10
by: Joe | last post by:
Hi, Tried using the FindControl() but no luck in finidng this damn textbox having id txtFirstName. Can someone help me with this method? This is what I have been doing but it doesn't work, ...
12
by: ThyRock | last post by:
I am working on a WebRequest accessing the US Postal Service WebTools test API. This service uses a DLL file (ShippingAPITest.dll) with a query string which includes XML. The web service accepts...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
8
by: Chris Noble | last post by:
I need to check whether a particular user is already a member of an Active Directory Security Group. The following code extract works but only if the user distinguished name is exactly the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.