473,387 Members | 1,582 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.

member function reference when extending an STL class

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?

Jul 23 '05 #1
4 1363
Alan wrote:
[...]
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
Jul 23 '05 #2
Alan wrote:
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::ostream & ostr ) const {
for ( IntList::const_iterator iter = this->begin();
iter != this->end(); ++iter ) {
ostr << *iter << ' ';
}
}

void write_alt ( std::ostream & 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
Jul 23 '05 #3
I tired putting parentheses around "*this", but that did not work
either. Same error.

Jul 23 '05 #4
Alan wrote:
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)[i] << ' '; // ****************
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
Jul 23 '05 #5

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
2
by: Husam | last post by:
Hi EveryBody: How can I add Sub or Function or property to be a member of TextBox ? For example TextBox1.Text , text is a property in Textbox1 How Can I add the following sub to be a member of...
2
by: Alex Vinokur | last post by:
If a class contains a member that is a pointer, one should implement copy constructor. Should one implement copy constructor if a class contains a member that is a reference? -- Alex...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: 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
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,...
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...

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.