T
Tony Johansson
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
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