By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,277 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,277 IT Pros & Developers. It's quick & easy.

STL, Handle-Body,templates

P: n/a

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.