member function reference when extending an STL class

A

Alan

I have a class, "event_list", derived from the template class "vector."
I extended it with a method, "write" for this new class. Here's its
definition:

class event_list : public vector<Event>
{
public:
void write (ofstream &filename, format_type format, int start, int
end);
};

For what it`s worth, the class "Event" is defined thus:

class Event
{
public:
int date;
string name;
string poc;
string type;
Event();
~Event();
void write(ofstream &filename, int start, int end);
};

In the "write" method for "event_list" (derived from vector), I want to
go through every element and write it out. My problem is how to
reference vector methods and operators within this method for the
derived class. I currently have the following code, which will not
compile:

void write (ofstream &filename, format_type format, int start, int end)

{
// write out vector contents
for (int indx = 0; indx < *this.size(); indx++)
{
current_date = *this[indx].date;
if (current_date != last_date)
{
last_date = current_date;
}
*this[indx].write(filename, start, end);
.. . .
};

However, the compiler is telling me that this is and invalid use of
"this" in non-member function (all 3 uses of "this"). I thought this
was the way to do it, since .size() is a function of the STL container
class vector, and "event_list" should inherit this member function.

Could anyone point out what I am doing wrong?
 
V

Victor Bazarov

Alan said:
[...]
void write (ofstream &filename, format_type format, int start, int
end)

{
// write out vector contents
for (int indx = 0; indx < *this.size(); indx++)
{
current_date = *this[indx].date;

current_data = (*this)[indx].date;

if (current_date != last_date)
{
last_date = current_date;
}
*this[indx].write(filename, start, end);

(*this)[indx].write(filename, start, end);

V
 
K

Kai-Uwe Bux

Alan said:
I have a class, "event_list", derived from the template class "vector."
I extended it with a method, "write" for this new class. Here's its
definition:

class event_list : public vector<Event>
{
public:
void write (ofstream &filename, format_type format, int start, int
end);
};
[snip]

In the "write" method for "event_list" (derived from vector), I want to
go through every element and write it out. My problem is how to
reference vector methods and operators within this method for the
derived class. I currently have the following code, which will not
compile:
[snip]

Could anyone point out what I am doing wrong?

a) Some people frown upon deriving from std::vector. Note that the
destructor of std::vector is not virtual. Thus, using a pointer
to std::vector polymorphically will usually not work as intended.
Beware of that.

b) From your code, it is not clear at all why write() should be a method
and not a static function that takes a stream and a std::vector<Event>
as arguments.

c) As for your problem, have a look at the following code. In
particular, note the use of parentheses in write_alt(). As
a matter of design, however, I would prefer the method write():
you could replace std::vector by std::list and the code would
still work. Thus, write() is more generic than write_alt().

#include <iostream>
#include <vector>

class IntList : public std::vector< int > {
public:

void write ( std::eek:stream & ostr ) const {
for ( IntList::const_iterator iter = this->begin();
iter != this->end(); ++iter ) {
ostr << *iter << ' ';
}
}

void write_alt ( std::eek:stream & ostr ) const {
for ( IntList::size_type index = 0;
index < (*this).size(); ++ index ) {
ostr << (*this)[index] << ' ';
}
}

}; // class IntList

int main ( void ) {
IntList i_list;
for ( int i = 0; i < 10; ++i ) {
i_list.push_back( i );
}
i_list.write( std::cout );
std::cout << '\n';
i_list.write_alt( std::cout );
}


Best

Kai-Uwe Bux
 
V

Victor Bazarov

Alan said:
I tired putting parentheses around "*this", but that did not work
either. Same error.

#include <iostream>
#include <vector>

using namespace std; // never do this in a public place

class event_list : public vector<int>
{
public:
void write(ostream& os) const
{
for (int i = 0; i < size(); i++)
os << (*this) << ' '; // ****************
os << endl;
}
};

int main()
{
event_list el;
el.push_back(1);
el.push_back(2);
el.push_back(4);

el.write(cout);
}


Seems to work just fine...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top