473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help deriving from the std::list class

I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?
Jul 22 '05 #1
8 2841
JustSomeGuy wrote:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?


You will not be able to _override_ any members because none of them
are virtual.

I recommend private inheritance (or what in OO world is known as
"implement in terms of" relationship). You will need to implement
the interface you need and if the objects are smaller than 1K, let
the underlying std::list handle them in memory, and if they are
greater, do something else (like retrieve them from disk based on
the information your underlying list keeps).

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
JustSomeGuy wrote:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?


You will not be able to _override_ any members because none of them
are virtual.

I recommend private inheritance (or what in OO world is known as
"implement in terms of" relationship). You will need to implement
the interface you need and if the objects are smaller than 1K, let
the underlying std::list handle them in memory, and if they are
greater, do something else (like retrieve them from disk based on
the information your underlying list keeps).

Victor


I tried this and it works...

class mylist : public list<int>
{
public:
void push_back(const int & x)
{
list<int>::push _back(x);
}
};

Compiles and works .... Has this not overriden push_back?
What other methods need to be overridden?
Jul 22 '05 #3
JustSomeGuy wrote:
Victor Bazarov wrote:

JustSomeGuy wrote:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?
You will not be able to _override_ any members because none of them
are virtual.

I recommend private inheritance (or what in OO world is known as
"implement in terms of" relationship). You will need to implement
the interface you need and if the objects are smaller than 1K, let
the underlying std::list handle them in memory, and if they are
greater, do something else (like retrieve them from disk based on
the information your underlying list keeps).

Victor

I tried this and it works...

class mylist : public list<int>
{
public:
void push_back(const int & x)
{
list<int>::push _back(x);
}


You don't need this for your 'mylist' to function as a list of
ints. Since you inherit publicly, whenever you call 'push_back'
for your 'mylist' object, the std::list::push _back is going to
be called.
};

Compiles and works .... Has this not overriden push_back?
No, it has not overridden push_back. Only virtual functions can be
overridden. What your 'push_back' has done is called _hiding_ of
the original push_back function.
What other methods need to be overridden?


I guess you don't understand the difference in terms. "Override"
only applies to virtual functions. Since 'std::list' has none,
you _cannot_ override anything. No matter what you try, it is not
going to be "overriding ".

Now, if you don't care for your special list to work polymorphically ,
you shouldn't worry about your inability to override anything. Just
write the functions you need and then let the std::list do the rest.

Keep in mind that your list is not going to work polymorphically .
If by some chance you write

class MyOwnListOfBigS caryObjects :
public std::list<MyBig ScaryObject> {
...
};

void foo(std::list<M yBigScaryObject >& mylist) {
mylist.push_bac k(someScaryObje ct);
}

that even if you define your own push_back member in your class,
it will NOT be called. I am not trying to talk you out of deriving
from 'std::list', just warning you of potential pitfalls.

Victor
Jul 22 '05 #4
Victor Bazarov wrote:
JustSomeGuy wrote:
Victor Bazarov wrote:

JustSomeGuy wrote:

I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?

You will not be able to _override_ any members because none of them
are virtual.

I recommend private inheritance (or what in OO world is known as
"implement in terms of" relationship). You will need to implement
the interface you need and if the objects are smaller than 1K, let
the underlying std::list handle them in memory, and if they are
greater, do something else (like retrieve them from disk based on
the information your underlying list keeps).

Victor

I tried this and it works...

class mylist : public list<int>
{
public:
void push_back(const int & x)
{
list<int>::push _back(x);
}


You don't need this for your 'mylist' to function as a list of
ints. Since you inherit publicly, whenever you call 'push_back'
for your 'mylist' object, the std::list::push _back is going to
be called.
};

Compiles and works .... Has this not overriden push_back?


No, it has not overridden push_back. Only virtual functions can be
overridden. What your 'push_back' has done is called _hiding_ of
the original push_back function.


Ok perhaps this is just a question of symantics then overridding vs hinding...
What other methods need to be overridden?
I guess you don't understand the difference in terms. "Override"
only applies to virtual functions. Since 'std::list' has none,
you _cannot_ override anything. No matter what you try, it is not
going to be "overriding ".

Now, if you don't care for your special list to work polymorphically ,
you shouldn't worry about your inability to override anything. Just
write the functions you need and then let the std::list do the rest.


Ok what functions do I 'need' to write? I think that is the original
question.
Yes they are very big objects on average the list grows to 40 Mega-Bytes.

Keep in mind that your list is not going to work polymorphically .
If by some chance you write

Are you saying that no one will be able to treat my class as a stl sdt::list?


class MyOwnListOfBigS caryObjects :
public std::list<MyBig ScaryObject> {
...
};

void foo(std::list<M yBigScaryObject >& mylist) {
mylist.push_bac k(someScaryObje ct);
}

that even if you define your own push_back member in your class,
it will NOT be called. I am not trying to talk you out of deriving
from 'std::list', just warning you of potential pitfalls.

I apprieate the warning... :)

Victor


Jul 22 '05 #5
JustSomeGuy wrote:
Victor Bazarov wrote:

JustSomeGuy wrote:
Victor Bazarov wrote:

JustSomeG uy wrote:
>I need to write an new class derived from the list class.
>This class stores data in the list to the disk if an object
>that is added to the list is over 1K in size.
>
>What methods of the std stl list class must Ioverride in order for this
>to work?

You will not be able to _override_ any members because none of them
are virtual.

I recommend private inheritance (or what in OO world is known as
"implemen t in terms of" relationship). You will need to implement
the interface you need and if the objects are smaller than 1K, let
the underlying std::list handle them in memory, and if they are
greater, do something else (like retrieve them from disk based on
the information your underlying list keeps).

Victor
I tried this and it works...

class mylist : public list<int>
{
public:
void push_back(const int & x)
{
list<int>::push _back(x);
}
You don't need this for your 'mylist' to function as a list of
ints. Since you inherit publicly, whenever you call 'push_back'
for your 'mylist' object, the std::list::push _back is going to
be called.

};

Compiles and works .... Has this not overriden push_back?


No, it has not overridden push_back. Only virtual functions can be
overridden. What your 'push_back' has done is called _hiding_ of
the original push_back function.

Ok perhaps this is just a question of symantics then overridding vs hinding...


Semantics is integral part of any language. English or C++. It is
important to understand the difference even if only to ignore it.
What other methods need to be overridden?


I guess you don't understand the difference in terms. "Override"
only applies to virtual functions. Since 'std::list' has none,
you _cannot_ override anything. No matter what you try, it is not
going to be "overriding ".

Now, if you don't care for your special list to work polymorphically ,
you shouldn't worry about your inability to override anything. Just
write the functions you need and then let the std::list do the rest.

Ok what functions do I 'need' to write?


How should *I* know? You're going to be using the class, not I.
I think that is the original
question.
But who except you can answer this?

If you want your class to be used by standard algorithms for searching,
sorting, etc., you need to provide iterators to the contents of your
collection. I strongly recommend a good book on the standard library
(Nicolai Josuttis wrote the best so far). The margins of this posting
are not wide enough to accommodate a decent explanation on how to write
your own library-compliant container.
Yes they are very big objects on average the list grows to 40 Mega-Bytes.


I believe you.
Keep in mind that your list is not going to work polymorphically .
If by some chance you write

Are you saying that no one will be able to treat my class as a stl sdt::list?


What do you mean by "treat" your class as std::list? You cannot pass
a pointer or a reference to your class to another function that expects
a pointer or a reference to std::list, and have that other function call
member functions of _your_ class. If the functions are not virtual (and
they are not in std::list), they are not going to be resolved to your
"variations ".

OTOH, if the other function is expecting _your_ list (either as a pointer
or as a reference, don't try passing it by value), then everything should
be fine.

In general you should remember that if you want the rest of the world to
be able to think that your class is just like std::list, your class must
have "is-a" relationship with std::list. However, if you want some of
std::list behaviour substituted for your own in certain conditions, you
need polymorphism, and none of standard containers provide that. That's
why I said that you'd be better off reimplementing everything you need
while deriving from std::list privately. At least then you will be able
to prevent people from passing your wonderful class into functions that
expect std::list (private inheritance disables implicit conversion to
base).
class MyOwnListOfBigS caryObjects :
public std::list<MyBig ScaryObject> {
...
};

void foo(std::list<M yBigScaryObject >& mylist) {
mylist.push_bac k(someScaryObje ct);
}

that even if you define your own push_back member in your class,
it will NOT be called. I am not trying to talk you out of deriving
from 'std::list', just warning you of potential pitfalls.

I apprieate the warning... :)


You're welcome.
Jul 22 '05 #6
JustSomeGuy <No***@ucalgary .ca> wrote in message news:<40******* ********@ucalga ry.ca>...
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?


std::list is not a class, it is a template. When you instantiate it by
providing arguments to the template parameters, you get a class.

!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!
To iterate is human, to recurse divine.
-L. Peter Deutsch
!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!
Jul 22 '05 #7
On Mon, 07 Jun 2004 13:40:03 -0600, JustSomeGuy <No***@ucalgary .ca>
wrote:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?


I would suggest not inheriting from std::list at all, but providing
your own limited list interface, where you can intercept the "size" of
intercepted objects more easily. With std::list, there are many ways
of adding elements to the list, and many of them can't be intercepted.
e.g.

*mylist.begin() = myenormousobjec t;
//your list type never gets to intercept that
or:
std::list& l = mylist;
l.push_back(mye normousobject);
//your list type never gets to intercept that

Instead, provide a limited interface that gives you the control you
need:

class MyList
{
std::list<MyObj ect> m_list; //or use private inheritence
public:
const_iterator begin() const;
const_iterator end() const;
//don't provide non-const iterators.

void push_back(MyObj ect const& object);

//add a few more functions, but how many do you need?
};

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8
JustSomeGuy <No***@ucalgary .ca> wrote in message news:<40******* ********@ucalga ry.ca>...
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.

What methods of the std stl list class must Ioverride in order for this
to work?


Actually, you probably shouldn't try to override list. Instead, you should
proxy the class you put in.

E.g. instead of
std::list<MyCla ss> list;
you write
std::list<MyCla ssProxy> list;

Example definitions for MyClass and MyClassProxy:

class MyClass {
std::string sometimes_big;
public:
std::string get() const;
}
class MyClassProxy {
bool in_file;
std::string filename_or_sma llstring;
public:
MyClassProxy( MyClass const& mc ) {
filename_or_sma llstring = mc.get();
in_file = false;
if( filename_or_sma llstring.size() >= 1024 )
{
filename_or_sma llstring = save_to_newfile (filename_or_sm allstring);
in_file = true;
}
}
std::string get() const {
if(infile)
return read_from_file( filename_or_sma llstring);
else
return filename_or_sma llstring;
}
operator MyClass() const { return MyClass(get()); }
private:
std::string save_to_newfile ( std::string const& s ); // returns filename
std::string read_from_file ( std::string const& filename );
};
Jul 22 '05 #9

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

Similar topics

14
5629
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
11
2270
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to next, if it's ready I take steps to delete it. The thing is that the list is part of a gui application and the system removes the Document* from the list during the execution of the loop body. That means I cant use iterators to iterate over the list....
7
2599
by: Gernot Frisch | last post by:
class A { public: std::list<A> m_a; }; yields errorC2079: '_Value' uses undefined class 'A' using std::vector will work...
6
6661
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
2
6026
by: sidaf | last post by:
Hello, I'm writing a connection management class, but I'm running into problems with the std::list template. I want to use a list to help manage my Connection objects. The ConnectionManager header file has the following inside the class definition: private:
4
2099
by: Thomas Grund | last post by:
Hi, In the following code I would like that the class Node is only be used by the class Database. So the idea was to make the interface protected and use the friend definition. The code does not compile because the list needs an public destructor. Making list<Nodea friend of Node doesn't help. If the constructor and destructor in Node is moved to the public section then everything works but this is not what I want. Thanks a lot,
7
2351
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
12
2707
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
4185
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.