Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > auto_ptr and casting

Reply
Thread Tools

auto_ptr and casting

 
 
James Juno
Guest
Posts: n/a
 
      12-16-2005
Hello,

I want to do something like this:

class A
{
...
};

class B : public A
{
...
};

void someFunction(const std::auto_ptr<A>& ptr)
{
B* b = dynamic_cast<B*>(ptr.get());
...
}

Is what I've done here the correct/safe way to handle this using auto_ptr?

Thanks!
JJ
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2005
* James Juno:
>
> I want to do something like this:
>
> class A
> {
> ...
> };
>
> class B : public A
> {
> ...
> };
>
> void someFunction(const std::auto_ptr<A>& ptr)
> {
> B* b = dynamic_cast<B*>(ptr.get());
> ...
> }
>
> Is what I've done here the correct/safe way to handle this using auto_ptr?


No, you cannot pass std::auto_ptr by reference to const.

For dynamic_cast class A must be polymorphic.

It's not clear what you're trying to do.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-16-2005
James Juno wrote:
> Hello,
>
> I want to do something like this:
>
> class A
> {
> ...
> };
>
> class B : public A
> {
> ...
> };
>
> void someFunction(const std::auto_ptr<A>& ptr)
> {
> B* b = dynamic_cast<B*>(ptr.get());
> ...
> }
>
> Is what I've done here the correct/safe way to handle this using auto_ptr?
>
> Thanks!
> JJ


No. First of all dynamic_cast can indicate poor design. Re-examine
yours. Second, the function should look like this:

void someFunction( std::auto_ptr<A>& a )
{
std::auto_ptr<B> b( dynamic_cast<B*>(ptr.release()) );
...
}

You'll also want to check if b.get() is null, in which case the cast
failed.

Cheers! --M

 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      12-16-2005
Alf P. Steinbach wrote:
> * James Juno:
>> I want to do something like this:
>>
>> class A
>> {
>> ...
>> };
>>
>> class B : public A
>> {
>> ...
>> };
>>
>> void someFunction(const std::auto_ptr<A>& ptr)
>> {
>> B* b = dynamic_cast<B*>(ptr.get());
>> ...
>> }
>>
>> Is what I've done here the correct/safe way to handle this using auto_ptr?

>
> No, you cannot pass std::auto_ptr by reference to const.
>
> For dynamic_cast class A must be polymorphic.
>
> It's not clear what you're trying to do.
>


I got the idea to pass as const reference from "The C++ Standard
Library" by N. Josuttis. By the way, this compiles and seems to work
fine in vc.net, I just didn't know if it was "safe." The auto_ptr
variable's type is set at runtime in the caller (one of several possible
decendants of A) so I never know what I'm getting in the call to
someFunction(). I am just using dynamic_cast as a debug check. Sorry
for being unclear, a better explanation of this function might be:

void someFunction(int type, const std::auto_ptr<A&> ptr)
{
// <type> is an enum defined elsewhere
switch (type)
{
case B:
#ifdef _DEBUG
B* b = dynamic_cast<B*>(ptr.get());
assert(b);
#else
B* b = static_cast<B*>(ptr.get());
#endif

...
case C:
... etc
}
}
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      12-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alf P. Steinbach) wrote in
news:(E-Mail Removed):

> * James Juno:
>>
>> I want to do something like this:
>>
>> class A
>> {
>> ...
>> };
>>
>> class B : public A
>> {
>> ...
>> };
>>
>> void someFunction(const std::auto_ptr<A>& ptr)
>> {
>> B* b = dynamic_cast<B*>(ptr.get());
>> ...
>> }
>>
>> Is what I've done here the correct/safe way to handle this using
>> auto_ptr?

>
> No, you cannot pass std::auto_ptr by reference to const.


Huh? Why not? As long as no modifying operations are called on ptr....
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      12-16-2005
"mlimber" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> James Juno wrote:
>> Hello,
>>
>> I want to do something like this:
>>
>> class A
>> {
>> ...
>> };
>>
>> class B : public A
>> {
>> ...
>> };
>>
>> void someFunction(const std::auto_ptr<A>& ptr)
>> {
>> B* b = dynamic_cast<B*>(ptr.get());
>> ...
>> }
>>
>> Is what I've done here the correct/safe way to handle this using
>> auto_ptr?
>>
>> Thanks!
>> JJ

>
> No. First of all dynamic_cast can indicate poor design. Re-examine
> yours. Second, the function should look like this:
>
> void someFunction( std::auto_ptr<A>& a )
> {
> std::auto_ptr<B> b( dynamic_cast<B*>(ptr.release()) );
> ...
> }


Only if the OP is attempting to transfer ownership of the pointer into
someFunction. Perhaps the OP is only attempting to use the B object.

 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      12-16-2005
mlimber wrote:
> James Juno wrote:
>> Hello,
>>
>> I want to do something like this:
>>
>> class A
>> {
>> ...
>> };
>>
>> class B : public A
>> {
>> ...
>> };
>>
>> void someFunction(const std::auto_ptr<A>& ptr)
>> {
>> B* b = dynamic_cast<B*>(ptr.get());
>> ...
>> }
>>
>> Is what I've done here the correct/safe way to handle this using auto_ptr?
>>
>> Thanks!
>> JJ

>
> No. First of all dynamic_cast can indicate poor design. Re-examine
> yours. Second, the function should look like this:
>
> void someFunction( std::auto_ptr<A>& a )
> {
> std::auto_ptr<B> b( dynamic_cast<B*>(ptr.release()) );
> ...
> }
>
> You'll also want to check if b.get() is null, in which case the cast
> failed.
>
> Cheers! --M
>


Perfect, thanks! That's exactly what I was looking for. The
dynamic_cast I'm using if just for debugging, sort of like:

void someFunction(int type, const std::auto_ptr<A>& ptr)
{
// <type> is an enum defined elsewhere
switch (type)
{
case B:

#ifdef _DEBUG
B* b = dynamic_cast<B*>(ptr.get());
assert(b != 0);
#else
B* b = static_cast<B*>(ptr.get());
#endif

...
case C:
... etc
}
}
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2005
* James Juno:
> Alf P. Steinbach wrote:
> > * James Juno:
> >> I want to do something like this:
> >>
> >> class A
> >> {
> >> ...
> >> };
> >>
> >> class B : public A
> >> {
> >> ...
> >> };
> >>
> >> void someFunction(const std::auto_ptr<A>& ptr)
> >> {
> >> B* b = dynamic_cast<B*>(ptr.get());
> >> ...
> >> }
> >>
> >> Is what I've done here the correct/safe way to handle this using auto_ptr?

> >
> > No, you cannot pass std::auto_ptr by reference to const.
> >
> > For dynamic_cast class A must be polymorphic.
> >
> > It's not clear what you're trying to do.
> >

>
> I got the idea to pass as const reference from "The C++ Standard
> Library" by N. Josuttis.


Well, correction: you cannot pass a temporary (such as a function result) as
argument here (technical reason, not allowed by the standard), and you
shouldn't pass a variable here (style issue, passing auto_ptr implies
passing ownership, which doesn't occur when passing by reference).


> By the way, this compiles and seems to work fine in vc.net,


MSVC is mostly incorrect wrt. std::auto_ptr.

Regarding casting, consider using base class virtual functions, which you
need anyway for dynamic_cast.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-16-2005

James Juno wrote:
> mlimber wrote:
> > James Juno wrote:
> >> Hello,
> >>
> >> I want to do something like this:
> >>
> >> class A
> >> {
> >> ...
> >> };
> >>
> >> class B : public A
> >> {
> >> ...
> >> };
> >>
> >> void someFunction(const std::auto_ptr<A>& ptr)
> >> {
> >> B* b = dynamic_cast<B*>(ptr.get());
> >> ...
> >> }
> >>
> >> Is what I've done here the correct/safe way to handle this using auto_ptr?
> >>
> >> Thanks!
> >> JJ

> >
> > No. First of all dynamic_cast can indicate poor design. Re-examine
> > yours. Second, the function should look like this:
> >
> > void someFunction( std::auto_ptr<A>& a )
> > {
> > std::auto_ptr<B> b( dynamic_cast<B*>(ptr.release()) );
> > ...
> > }
> >
> > You'll also want to check if b.get() is null, in which case the cast
> > failed.
> >
> > Cheers! --M
> >

>
> Perfect, thanks! That's exactly what I was looking for. The
> dynamic_cast I'm using if just for debugging, sort of like:
>
> void someFunction(int type, const std::auto_ptr<A>& ptr)
> {
> // <type> is an enum defined elsewhere
> switch (type)
> {
> case B:
>
> #ifdef _DEBUG
> B* b = dynamic_cast<B*>(ptr.get());
> assert(b != 0);
> #else
> B* b = static_cast<B*>(ptr.get());
> #endif
>
> ...
> case C:
> ... etc
> }
> }


On second thought, the code I supplied would leak memory if the cast
failed. Try this:

void someFunction( std::auto_ptr<A>& a )
{
A* const rawA = a.release();
std::auto_ptr<B> b( dynamic_cast<B*>(rawA) );
if( b.get() )
{
...
}
else
{
delete rawA;
}
}

Cheers! --M

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2005
* Andre Kostur:
> (E-Mail Removed) (Alf P. Steinbach) wrote in
> news:(E-Mail Removed):
>
> > * James Juno:
> >>
> >> I want to do something like this:
> >>
> >> class A
> >> {
> >> ...
> >> };
> >>
> >> class B : public A
> >> {
> >> ...
> >> };
> >>
> >> void someFunction(const std::auto_ptr<A>& ptr)
> >> {
> >> B* b = dynamic_cast<B*>(ptr.get());
> >> ...
> >> }
> >>
> >> Is what I've done here the correct/safe way to handle this using
> >> auto_ptr?

> >
> > No, you cannot pass std::auto_ptr by reference to const.

>
> Huh? Why not? As long as no modifying operations are called on ptr....


You cannot pass a temporary as actual argument because std::auto_ptr does
not have a copy constructor taking std::auto_ptr by reference to const.

You can but shouldn't pass a variable as actual argument.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments