473,387 Members | 1,300 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,387 software developers and data experts.

How to use the iterator from base class in a derived class?

I am trying to write a class myVector based on std::vector and add
only a sort method. The sort method needs to access the array stored
in the base class using iterator. I have following draft code. However
it never compile. Can anyone take a look and tell me how to fix it?
Thanks!

#include <vector>
#include <iostream>

using namespace std;

template <typename Tclass myVector : public vector<T>
{
public:
void Sort();
};

template <typename Tvoid Sort<T>::Sort()
{
if (this.size() < 2)
{
return;
}

vector<T>::iterator i;

for (i = this.begin(); i != this.end(); i++)
{
//rest part ignored....
}
}

Oct 18 '07 #1
7 3732
On 2007-10-18 09:29, wangxiaohu wrote:
I am trying to write a class myVector based on std::vector and add
only a sort method. The sort method needs to access the array stored
in the base class using iterator. I have following draft code. However
it never compile. Can anyone take a look and tell me how to fix it?
Thanks!

#include <vector>
#include <iostream>

using namespace std;

template <typename Tclass myVector : public vector<T>
The general advice is to not inherit from the standard containers,
instead create a class that wraps the container.
{
public:
void Sort();
};

template <typename Tvoid Sort<T>::Sort()
template <typename Tvoid <myVector<T>::sort()
{
if (this.size() < 2)
this->size()

This is a pointer to the current object, you need to use the dereference
operator (->) when accessing members of the current object.

--
Erik Wikström
Oct 18 '07 #2
wangxiaohu wrote:
I am trying to write a class myVector based on std::vector and add
only a sort method.
BadIdea(tm). That is abuse of inheritance. You should use a free standing
function instead:

template < typename T, typename A >
void sort_vector ( std::vector<T,A& sequence ) {
std::sort( sequence.begin(), sequence.end() );
}

or maybe even more generic:

template < typename Container >
void sort_sequence ( Container & sequence ) {
std::sort( sequence.begin(), sequence.end() );
}

or with a concept check:

template < typename Container >
void sort_sequence
( Container & sequence,
typename enable_if
< is_sequence< Container >::value, void* >::type = 0 ) {
std::sort( sequence.begin(), sequence.end() );
}
There are some cases where public inheritance from std::vector<is
justified. This does not look like one of them.

The sort method needs to access the array stored
in the base class using iterator. I have following draft code. However
it never compile. Can anyone take a look and tell me how to fix it?
Thanks!

#include <vector>
#include <iostream>

using namespace std;
Don't put using directives in header files. Your decision to pull all
identifiers from the standard namespace is inflicted upon every user of
your class. That is very intrusive.

template <typename Tclass myVector : public vector<T>
{
public:
void Sort();
};

template <typename Tvoid Sort<T>::Sort()
Shouldn't that be something like

template <typename T>
void myVector::Sort()

{
if (this.size() < 2)
{
return;
}

vector<T>::iterator i;
You would need to say

typename std::vector<T>::iterator i;

The keyword here is "dependent name".

>
for (i = this.begin(); i != this.end(); i++)
{
//rest part ignored....
}
}
BTW: why are you doing your own sort algorithm? any why only for vectors? It
is _very_ hard to beat std::sort() from the header <algorithm>.
Best

Kai-Uwe Bux
Oct 18 '07 #3
I just want to see if I can add a Sort() method to the existing vector
container.

What you've shown is write a individual function, which does work for
the purpose, but does not answer my question.

But really thanks to your detailed example!

wxh

On 10 18 , 1 22 , Kai-Uwe Bux <jkherci...@gmx.netwrote:
wangxiaohu wrote:
I am trying to write a class myVector based on std::vector and add
only a sort method.

BadIdea(tm). That is abuse of inheritance. You should use a free standing
function instead:

template < typename T, typename A >
void sort_vector ( std::vector<T,A& sequence ) {
std::sort( sequence.begin(), sequence.end() );
}

or maybe even more generic:

template < typename Container >
void sort_sequence ( Container & sequence ) {
std::sort( sequence.begin(), sequence.end() );
}

or with a concept check:

template < typename Container >
void sort_sequence
( Container & sequence,
typename enable_if
< is_sequence< Container >::value, void* >::type = 0 ) {
std::sort( sequence.begin(), sequence.end() );
}

There are some cases where public inheritance from std::vector<is
justified. This does not look like one of them.
The sort method needs to access the array stored
in the base class using iterator. I have following draft code. However
it never compile. Can anyone take a look and tell me how to fix it?
Thanks!
#include <vector>
#include <iostream>
using namespace std;

Don't put using directives in header files. Your decision to pull all
identifiers from the standard namespace is inflicted upon every user of
your class. That is very intrusive.
template <typename Tclass myVector : public vector<T>
{
public:
void Sort();
};
template <typename Tvoid Sort<T>::Sort()

Shouldn't that be something like

template <typename T>
void myVector::Sort()
{
if (this.size() < 2)
{
return;
}
vector<T>::iterator i;

You would need to say

typename std::vector<T>::iterator i;

The keyword here is "dependent name".
for (i = this.begin(); i != this.end(); i++)
{
//rest part ignored....
}
}

BTW: why are you doing your own sort algorithm? any why only for vectors? It
is _very_ hard to beat std::sort() from the header <algorithm>.

Best

Kai-Uwe Bux

Oct 18 '07 #4
On 2007-10-20 16:58:47 -0400, Tristan Wibberley <ma********@maihem.orgsaid:
>
On Thu, 2007-10-18 at 08:04 +0000, Erik Wikström wrote:
>The general advice is to not inherit from the standard containers,
instead create a class that wraps the container.

I'd heard that was because somebody might use a reference to your object
with type std::vector<...>& and use all the wrong functions.
This doesn't make sense. I thought the whole point of Object-Oriented
Programming is that polymophisms are encouraged as a coding structure.

If a derived class can potentially ``damaged'' the integrity of the
base class, then as the designer of the base class he shouldn't have
allowed his methods be declared virtual (so that the derived class
could not override it). If so, how then can any reference of the base
class be using any wrong methods supplied by the derived class?
>
Is there any other problem that makes that a bad idea too?
Yea, I like to know too.

--

-kira

Oct 20 '07 #5

On Sat, 2007-10-20 at 18:02 -0400, Kira Yamato wrote:
... I thought the whole point of Object-Oriented
Programming is that polymophisms are encouraged as a coding structure.
Standard containers are not object oriented then.

In the standard C++ library it's basically locale and iostreams related
stuff that is OO (or at least has OO aspects), the rest is just abstract
data types.

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

Oct 20 '07 #6
On 2007-10-21 00:02, Kira Yamato wrote:
On 2007-10-20 16:58:47 -0400, Tristan Wibberley <ma********@maihem.orgsaid:
>>
On Thu, 2007-10-18 at 08:04 +0000, Erik Wikström wrote:
>>The general advice is to not inherit from the standard containers,
instead create a class that wraps the container.

I'd heard that was because somebody might use a reference to your object
with type std::vector<...>& and use all the wrong functions.

This doesn't make sense. I thought the whole point of Object-Oriented
Programming is that polymophisms are encouraged as a coding structure.

If a derived class can potentially ``damaged'' the integrity of the
base class, then as the designer of the base class he shouldn't have
allowed his methods be declared virtual (so that the derived class
could not override it). If so, how then can any reference of the base
class be using any wrong methods supplied by the derived class?
None of the standard library containers have any virtual functions, most
notably the destructor is not virtual. This is because they were not
designed to be inherited from.
>Is there any other problem that makes that a bad idea too?

Yea, I like to know too.
If the base class does not have a virtual destructor the derived class's
destructor will not be run when deleting a base class pointer pointing
to a derived object.

--
Erik Wikström
Oct 20 '07 #7

On Sun, 2007-10-21 at 12:00 +1300, Ian Collins wrote:
Tristan Wibberley wrote:
On Thu, 2007-10-18 at 08:04 +0000, Erik Wikström wrote:
The general advice is to not inherit from the standard containers,
instead create a class that wraps the container.
I'd heard that was because somebody might use a reference to your object
with type std::vector<...>& and use all the wrong functions. What about
private inheritance?

Is there any other problem that makes that a bad idea too?
Standard containers don't have virtual destructors.
That wouldn't be a concern with private inheritance since any pointer to
an instance of the container that might be "delete"ed *must* be
pointer-to-derived class and the correct destructor will be called -
unless the derived class exports a pointer to the base class (which
would be obviously problematic).

--
Tristan Wibberley

Any opinion expressed is mine (or else I'm playing devils advocate for
the sake of a good argument). My employer had nothing to do with this
communication.

Oct 20 '07 #8

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

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
4
by: propokergrad | last post by:
Hello, say I have two classes: class Base{...}; class Derived : public Base{...} I would like to do something similar to this: std::vector<Base*>::iterator b;...
7
by: Ralf Goertz | last post by:
Hi, the following templated code doesn't compile. It gives the error: template_it.cc:17: error: type 'std::vector<Derived1<T>*, std::allocator<Derived1<T>*' is not derived from type...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.