469,658 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

STL, Handle-Body,templates


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.
Each Body object contains one primitive datatype int.
The Body instance is created in the constructor for Handle. In main I
instansiate some Handle object and use the STL function push_front to push
these object into the list.

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.
The only way to get the values from the Body class is to use the
functionoperator defines as
operator int() const
{ return this->value_; }

I think the solution is somewhat to write a function in the Handle class
that use the functionoperator.
The function in Handle as I think should use the STL function for_each in
some way.

Have you some good solution to my problem?

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

include "handle.h"
#include <list>
#include <algorithm>
using namespace std;
int main()
list <Handle <Body> > myList1;

Handle<Body> myh1(1);

Handle<Body> myh2(2);

Handle<Body> myh3(3);
return 0;

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

#include <iostream>
class Body

Body(int value): value_(value)
copy_cnt = new int(0);
std::cout << "Creating original:"<< value << std::endl;

operator int() const
{ return this->value_; }

int value_;
int *copy_cnt;

Jul 23 '05 #1
0 1995

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Fred Hebert | last post: by
2 posts views Thread by Indiana Epilepsy and Child Neurology | last post: by
14 posts views Thread by Howard | last post: by
6 posts views Thread by Leandro Berti via DotNetMonster.com | last post: by
13 posts views Thread by Abhishek | last post: by
3 posts views Thread by Peterwkc | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.