Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > auto_ptr<Derived> to auto_ptr<Base>

Reply
Thread Tools

auto_ptr<Derived> to auto_ptr<Base>

 
 
Siemel Naran
Guest
Posts: n/a
 
      01-10-2005
This code fails compile

std::auto_ptr<Base> f() {
std::auto_ptr<Derived> out(new Derived());
return out;
}

There is ambiguity between a templated constructor and templated operator
conversion, according to my compiler. Seems there are too many constructors
and operator conversions. But this code works:

int main() {
std::auto_ptr<Derived> derived(new Derived());
std::auto_ptr<Base> base(derived);
return out;
}

Is my compiler broken? Why the difference between the two snippets of code.



 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      01-10-2005

"Siemel Naran" <(E-Mail Removed)> wrote in message
news:_vsEd.94345$(E-Mail Removed)...
> This code fails compile
>
> std::auto_ptr<Base> f() {
> std::auto_ptr<Derived> out(new Derived());
> return out;
> }
>
> There is ambiguity between a templated constructor and templated operator
> conversion, according to my compiler. Seems there are too many
> constructors
> and operator conversions. But this code works:
>
> int main() {


main??? Do you mean "std::auto_ptr<Base> f()"?

> std::auto_ptr<Derived> derived(new Derived());
> std::auto_ptr<Base> base(derived);
> return out;
> }
>
> Is my compiler broken? Why the difference between the two snippets of
> code.
>
>
>


What exactly are you trying to do? Looking at this code and the code in
your last post, it looks like you're trying to create a pointer to a Base
class object, initializing it with a Derived class object. But the code
above makes no sense.

Your previous code looked better to me, as far as creating the object. But
then you tried to call a function in that object which you made protected.
You can't do that, except from inside one of its own member functions (or
one of its own descendants).

So now it looks like to get around that problem, you're trying to force a
Derived object into a Base object.

I'd go back to your earlier design (assuming I'm understanding it
correctly). But I'd make the setvariable function public instead of
protected.

All this is based on speculation, however, since you're providing way too
little information. We need the real code (or at least a much more complete
sample of it), and exactly what it is you're trying to accomplish with the
code, before we could really help, I think.

-Howard



 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      01-11-2005
Siemel Naran wrote:
> This code fails compile
>
> std::auto_ptr<Base> f() {
> std::auto_ptr<Derived> out(new Derived());
> return out;
> }
>
> There is ambiguity between a templated constructor and templated operator
> conversion, according to my compiler. Seems there are too many constructors
> and operator conversions. But this code works:
>
> int main() {
> std::auto_ptr<Derived> derived(new Derived());
> std::auto_ptr<Base> base(derived);
> return out;
> }
>
> Is my compiler broken? Why the difference between the two snippets of code.


This works on my compiler (MSVC.net 2003)
---
#include <memory>

using std::auto_ptr;

class Base {};
class Derived: public Base {};

auto_ptr<Base> f()
{
auto_ptr<Derived> d(new Derived());
return d;
}
---

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




Advertisments