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

auto_ptr and casting

P: n/a
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
Dec 16 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
* 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?
Dec 16 '05 #2

P: n/a
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

Dec 16 '05 #3

P: n/a
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
}
}
Dec 16 '05 #4

P: n/a
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
}
}
Dec 16 '05 #5

P: n/a
* 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?
Dec 16 '05 #6

P: n/a

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

Dec 16 '05 #7

P: n/a
al***@start.no (Alf P. Steinbach) wrote in
news:43****************@news.individual.net:
* 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....
Dec 16 '05 #8

P: n/a
"mlimber" <ml*****@gmail.com> wrote in
news:11**********************@o13g2000cwo.googlegr 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.

Dec 16 '05 #9

P: n/a
Andre Kostur wrote:
"mlimber" <ml*****@gmail.com> wrote in
news:11**********************@o13g2000cwo.googlegr 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.


Yes, the OP is only trying to use the object. If style semantics dictate
that an auto_ptr as a function parameter should imply ownership
transfer, then what's better in this case? It's becoming more of a
headache to use auto_ptr than to just deal with resource management
manually.
Dec 16 '05 #10

P: n/a
* Andre Kostur:
al***@start.no (Alf P. Steinbach) wrote in
news:43****************@news.individual.net:
* 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?
Dec 16 '05 #11

P: n/a

James Juno wrote:
Andre Kostur wrote:
"mlimber" <ml*****@gmail.com> wrote in
news:11**********************@o13g2000cwo.googlegr 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.


Yes, the OP is only trying to use the object. If style semantics dictate
that an auto_ptr as a function parameter should imply ownership
transfer, then what's better in this case? It's becoming more of a
headache to use auto_ptr than to just deal with resource management
manually.


If you don't intend to transfer ownership to the function, then your
original code looks fine.

Cheers! --M

Dec 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.