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

STL and some Template

Hello!

Here I have two classes these are called Handle and Body and a main. You
have the class definition below. Some basic information. In the Handle class
is there a pointer to the Body class.
Each Body object contains one primitive datatype int.
The Body instance is created in the constructor for Handle. In main I
instanciate some Handle object and use the STL function push_front to push
these object into the list.

In main I also use the STL function for_each to access each node in the list
as you see here for_each(myList1.begin(), myList1.end(),
Handle<Integer>());

The last parameter is calling the functionsoperator defined in class Handle
for each node in the list.
In my case I have two nodes so this functionsoperator is called two times so
the text testing is printed two times. This works

The only way to get the primitive int values from the Body class is to use
the
type conversion operator defined as this
operator int() const
{ return this->value_; }
which convert from class Body to an int.

But now to my question how do I do if I want to display the entire list
myList1 with all the ints that exist in each created body object. I want to
check that my push_front is doing what I expect it to do.
So have anybody out there any suggestion how to access the type conversion
operator in class Body
to get the primitive int defined there.

I know that I must add destructor, copy constuctor and assignment operator
and all that stuff.

//Tony

Main
*******
include "handle.h"
#include <list>
#include <algorithm>
using namespace std;

int main()
{
list <Handle <Body> > myList1;

Handle<Body> myh1(1);
myList1.push_front(myh1);
Handle<Body> myh2(2);
myList1.push_front(myh2);

for_each(myList1.begin(), myList1.end(), Handle<Integer>());

return 0;
}

Handle
*******
#include "body.h"
#include <iostream>
using namespace std;
template<class T>
class Handle
{
public:
Handle(int value)
{ body = new T(value); }

void operator()(Handle<T>& temp)
{ cout << "testing" << endl; }

private:
Body *body;
};
#include <iostream>
class Body
{
public:
Body() {}

Body(int value): value_(value) {}

operator int() const
{ return this->value_; }
private:
int value_;
};
//Tony

Jul 23 '05 #1
0 967

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

Similar topics

0
by: Ray Tayek | last post by:
hi, trying to convert some csv files into xsml and pulling a few hairs out :(. using the files below. a java program will parse the csv and take care of strange names and notes that line breaks in...
1
by: Agoston Bejo | last post by:
Hello, I've got a template related problem. How do I express through template metaprogramming or in a similar way that a class is convertible to a template class for some T1? I.e., the condition:...
4
by: PengYu.UT | last post by:
Hi, "call()" calles "show()". I got some error as indicated below in the comments. I tried to delete the definition and declaration of "call()" and call "show()" in the main() body, then it got...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
0
by: JackWarner | last post by:
OK, I'm finally getting our organization to upgrade from v6, and the first project I try to recompile I run into hairy problems. We've used this ATL COM collection class for years. It looks like...
2
by: Eduardo Rosa | last post by:
I've a trouble with xml, I need to access "a node with child that had some specific text". Somebody can help me? Thanks a lot example: xsl file: <xsl:template match="Group"> <!-- that`s...
4
by: Hemal Pandya | last post by:
Hello, I am hoping this is a simple question with a straightforward solution. I do not understand xsl much, so I apologize in advance if I am asking a stupid question. Is it possible to write...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
2
by: erikcw | last post by:
Hi all, I'm trying to write a loop that will build a list of "template strings". My current implementation is *really slow*. It took 15 minutes to finish. (final len(list) was about 16k...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.