473,326 Members | 2,173 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,326 software developers and data experts.

boost:shared_ptr cast problem

Jun
Hello,

I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);

}

typedef boost::shared_ptr<AAPtr; // Define A class smart pointer

APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());

vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);

std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));

std::copy(AVec.begin(),AVec.end(), std::ostream_iterator< share_ptr<A>
>(std::cout, "\n"));
===========================================

The two copy methods only output the address of pointers, they could
just print the class A defined print contents. And following code
works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================

Anyone has some ideas for that ? Thank you in advance.
Jun
Nov 25 '07 #1
5 2867
Jun
On Nov 25, 10:12 pm, Jun <junh...@gmail.comwrote:
Hello,

I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);

}

typedef boost::shared_ptr<AAPtr; // Define A class smart pointer

APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());

vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);

std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));

std::copy(AVec.begin(),AVec.end(), std::ostream_iterator< share_ptr<A>>(std::cout, "\n"));

===========================================

The two copy methods only output the address of pointers, they could
just print the class A defined print contents. And following code
works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================

Anyone has some ideas for that ? Thank you in advance.

Jun
Actually, It's boost::shared_ptr serialization problem.
Nov 25 '07 #2
Jun wrote:
Hello,

I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);

}

typedef boost::shared_ptr<AAPtr; // Define A class smart pointer

APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());

vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);

std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));

std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator< share_ptr<A(std::cout, "\n"));
===========================================

The two copy methods only output the address of pointers, they could
just print the class A defined print contents.
Why would you expect that? Consider

int i = 5;
std::cout << i << std::endl
<< &i << std::endl;

You would expect the second line to print an address, would you not? Why
should shared_ptr<behave differently? Also note that shared_ptr could
have null value. In that case, there is no pointee that could be printed
instead of an address.

And following code works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================
Anyone has some ideas for that ?
Yes, leave out the cast: *i is already of type APtr.

cout << *(*i) << endl;

BTW: I have the feeling that I did not really understand what the real
problem is that you want to solve. Maybe, you oversimplified it for the
purpose of the post. Could you provide a little background as to _why_ you
want to print the shared_ptr and _why_ you feel it would be the
RightThing(tm) if that printed the pointee instead of an address? Maybe, it
is the context of the underlying problem that makes you think printing the
pointee would be right. In that case, it would be good to share the
underlying problem with us, because it might have a well known solution.
Best

Kai-Uwe Bux

Nov 26 '07 #3
Jun
On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Jun wrote:
Hello,
I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);
}
typedef boost::shared_ptr<AAPtr; // Define A class smart pointer
APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());
vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator< share_ptr<A(std::cout, "\n"));
===========================================
The two copy methods only output the address of pointers, they could
just print the class A defined print contents.

Why would you expect that? Consider

int i = 5;
std::cout << i << std::endl
<< &i << std::endl;

You would expect the second line to print an address, would you not? Why
should shared_ptr<behave differently? Also note that shared_ptr could
have null value. In that case, there is no pointee that could be printed
instead of an address.
And following code works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================
Anyone has some ideas for that ?

Yes, leave out the cast: *i is already of type APtr.

cout << *(*i) << endl;

BTW: I have the feeling that I did not really understand what the real
problem is that you want to solve. Maybe, you oversimplified it for the
purpose of the post. Could you provide a little background as to _why_ you
want to print the shared_ptr and _why_ you feel it would be the
RightThing(tm) if that printed the pointee instead of an address? Maybe, it
is the context of the underlying problem that makes you think printing the
pointee would be right. In that case, it would be good to share the
underlying problem with us, because it might have a well known solution.

Best

Kai-Uwe Bux
I've a person class, which contains name, age. I applied output as
name age,
for my custom class. By storing it as a smart pointer PersonPtr in a
vector.
Then my idea is using copy algorithm to print out all the PersonPtr in
the
vector, but failed. Since PersonPtr has the serialization problem. By
using
copy algorithm, it only prints the address of smart pointer instead
name age,
which i defined.

Nov 26 '07 #4
Jun wrote:
On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>Jun wrote:
Hello,
I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);
}
typedef boost::shared_ptr<AAPtr; // Define A class smart pointer
APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());
vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator< share_ptr<A(std::cout, "\n"));
===========================================
The two copy methods only output the address of pointers, they could
just print the class A defined print contents.

Why would you expect that? Consider

int i = 5;
std::cout << i << std::endl
<< &i << std::endl;

You would expect the second line to print an address, would you not? Why
should shared_ptr<behave differently? Also note that shared_ptr could
have null value. In that case, there is no pointee that could be printed
instead of an address.
And following code works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================
Anyone has some ideas for that ?

Yes, leave out the cast: *i is already of type APtr.

cout << *(*i) << endl;

BTW: I have the feeling that I did not really understand what the real
problem is that you want to solve. Maybe, you oversimplified it for the
purpose of the post. Could you provide a little background as to _why_
you want to print the shared_ptr and _why_ you feel it would be the
RightThing(tm) if that printed the pointee instead of an address? Maybe,
it is the context of the underlying problem that makes you think printing
the pointee would be right. In that case, it would be good to share the
underlying problem with us, because it might have a well known solution.

Best

Kai-Uwe Bux

I've a person class, which contains name, age. I applied output as
name age,
for my custom class. By storing it as a smart pointer PersonPtr in a
vector.
Then my idea is using copy algorithm to print out all the PersonPtr in
the
vector, but failed. Since PersonPtr has the serialization problem. By
using
copy algorithm, it only prints the address of smart pointer instead
name age,
which i defined.
You might try:

template < typename T >
T const & deref ( std::tr1::shared_ptr<Tptr ) {
return ( *ptr );
}

and

std::transform( AVec.begin(), AVec.end(),
std::ostream_iterator< A >( std::cout, "\n" ),
&deref<A);

Maybe, there is a way to use boost::lambda and just say

std::transform( AVec.begin(), AVec.end(),
std::ostream_iterator< A >( std::cout, "\n" ),
*_1);

without the need to define deref.
Best

Kai-Uwe Bux
Nov 26 '07 #5
Jun
On Nov 26, 1:10 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Jun wrote:
On Nov 26, 1:52 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Jun wrote:
Hello,
I've code like :
===========================================
class A{
public :
// create print content
friend std::ostream& operator<< (std::ostream&
os, const A& a);
}
typedef boost::shared_ptr<AAPtr; // Define A class smart pointer
APtr aPtr1(new A());
APtr aPtr2(new A());
APtr aPtr3(new A());
vector<APtrAVec;
AVec.push_back(aPtr1);
AVec.push_back(aPtr2);
AVec.push_back(aPtr3);
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator<APtr>(std::cout, "\n"));
std::copy(AVec.begin(),AVec.end(),
std::ostream_iterator< share_ptr<A(std::cout, "\n"));
===========================================
The two copy methods only output the address of pointers, they could
just print the class A defined print contents.
Why would you expect that? Consider
int i = 5;
std::cout << i << std::endl
<< &i << std::endl;
You would expect the second line to print an address, would you not? Why
should shared_ptr<behave differently? Also note that shared_ptr could
have null value. In that case, there is no pointee that could be printed
instead of an address.
And following code works :
===========================================
vector<APtr>::iterator i = AVec.begin();
for(; i!= AVec.end(); ++i){
cout << *static_cast<APtr>(*i) << endl;
}
===========================================
Anyone has some ideas for that ?
Yes, leave out the cast: *i is already of type APtr.
cout << *(*i) << endl;
BTW: I have the feeling that I did not really understand what the real
problem is that you want to solve. Maybe, you oversimplified it for the
purpose of the post. Could you provide a little background as to _why_
you want to print the shared_ptr and _why_ you feel it would be the
RightThing(tm) if that printed the pointee instead of an address? Maybe,
it is the context of the underlying problem that makes you think printing
the pointee would be right. In that case, it would be good to share the
underlying problem with us, because it might have a well known solution.
Best
Kai-Uwe Bux
I've a person class, which contains name, age. I applied output as
name age,
for my custom class. By storing it as a smart pointer PersonPtr in a
vector.
Then my idea is using copy algorithm to print out all the PersonPtr in
the
vector, but failed. Since PersonPtr has the serialization problem. By
using
copy algorithm, it only prints the address of smart pointer instead
name age,
which i defined.

You might try:

template < typename T >
T const & deref ( std::tr1::shared_ptr<Tptr ) {
return ( *ptr );

}

and

std::transform( AVec.begin(), AVec.end(),
std::ostream_iterator< A >( std::cout, "\n" ),
&deref<A);

Maybe, there is a way to use boost::lambda and just say

std::transform( AVec.begin(), AVec.end(),
std::ostream_iterator< A >( std::cout, "\n" ),
*_1);

without the need to define deref.

Best

Kai-Uwe Bux
I saw the lambda solution, anyway, I will try and post the results.
Jun
Nov 26 '07 #6

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

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point -...
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.