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
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.
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.

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);

Handle<Body> myh3(3);
myList1.push_front(myh3);
return 0;
}

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

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

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

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

private:
int value_;
int *copy_cnt;
};

//Tony
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.