auto_ptr<Derived> to auto_ptr<Base>

S

Siemel Naran

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.
 
H

Howard

Siemel Naran said:
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 said:
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
 
D

Dave Rahardja

Siemel said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top