473,396 Members | 2,017 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,396 software developers and data experts.

Best way to "redefine" a std class

Hello I want to define a class myList, which is the same that std::list
except from:
- insert (I want to redefine it)
- elements can only be consulted or deleted but not modified.
- I want

Which is the best way to do that?:

1.
template <typename T> myList : public list<T> {
public:
insert(...) { ... }
...
// what about iterators?
};

OR

2.

template <typename T> myList {
private:
list<T> theList;
public:
// all the methos of list addapted to myList
// i.e.:
int size() {return theList.size(); }
...
// what about iterators?
};

Jul 22 '05 #1
4 2301

Nafai wrote:
Hello I want to define a class myList, which is the same that std::list except from:
- insert (I want to redefine it)
- elements can only be consulted or deleted but not modified.
- I want

Which is the best way to do that?:

1. (public inheritance of std::list) OR

2.

(private std::list member, i.e. wrap)

Easy, you must wrap. The std::list part must be private, otherwise
one can easily modify the list.

Consider the function foo( std::list<int>& );

Can you pass an instance of your list? No, so inheritance is out.
Regards,
Michiel Salters

Jul 22 '05 #2
...as for the iterators - just typedef them:

template <typename T> myList {
public:
typedef std::list<T> MyListType;
MyListType::const_iterator const_iterator;

const_iterator begin() const { return theList.begin(); }
const_iterator end() const { return theList.end(); }

private:
MyListType theList;
MyListType::iterator iterator;
iterator begin(){ return theList.begin(); }
iterator end() { return theList.end(); }
};

"Nafai" <na*******@yahoo.es> wrote in message
news:Mo***********************@telenews.teleline.e s...
Hello I want to define a class myList, which is the same that std::list
except from:
- insert (I want to redefine it)
- elements can only be consulted or deleted but not modified.
- I want

Which is the best way to do that?:

1.
template <typename T> myList : public list<T> {
public:
insert(...) { ... }
...
// what about iterators?
};

OR

2.

template <typename T> myList {
private:
list<T> theList;
public:
// all the methos of list addapted to myList
// i.e.:
int size() {return theList.size(); }
...
// what about iterators?
};


Jul 22 '05 #3
In general, C++ classes which were intended to be inherited from, have
virtual destructors. This is something the STL containers do not have
and so were not intended to serve as base classes.

Jul 22 '05 #4
In general, C++ classes which were intended to be inherited from, have
virtual destructors. This is something the STL containers do not have
and so were not intended to serve as base classes.

Jul 22 '05 #5

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

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
3
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
10
by: Özden Irmak | last post by:
Hi, I'm trying to hide an event of my custom usercontrol derived control, something like "Shadows" in VB.Net, but although the event is hidden from PropertyGrid, from CodeEditor I can still...
5
by: crystalattice | last post by:
I've finally figured out the basics of OOP; I've created a basic character creation class for my game and it works reasonably well. Now that I'm trying to build a subclass that has methods to...
2
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
2
by: jeff123 | last post by:
Hi all, Recently I started using C#, I have a question: Can we redefine a class legally in C# ? For example: private void button1_Click(object sender, EventArgs e) {...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.