473,799 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1380
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<Eve nt>
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_t ype 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_bac k( i );
}
i_list.write( std::cout );
std::cout << '\n';
i_list.write_al t( 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
9377
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, 2, func);
12
2093
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, so he will send us all on a conversion course. One of many reasons is that our code is littered with examples of: SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE) IF (ITEM_TYPE .EQ. SQUARE) THEN CALL PRINT_SQUARE(ITEM)
39
3101
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
4412
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
2
1191
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 textbox1: Public Sub SaveHtml(ByVal stream As Stream) If stream Is Nothing Then Throw New ArgumentNullException("SaveHtml : Must specify a
2
1935
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 Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
7
2805
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 object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass << cout; someclass >cin; ?
5
4665
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
4121
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
10484
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...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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
5463
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.