473,886 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto_ptr<Derive d> to auto_ptr<Base>

This code fails compile

std::auto_ptr<B ase> f() {
std::auto_ptr<D erived> 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<D erived> derived(new Derived());
std::auto_ptr<B ase> base(derived);
return out;
}

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

Jul 22 '05 #1
2 2111

"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:_v******** ***********@bgt nsc05-news.ops.worldn et.att.net...
This code fails compile

std::auto_ptr<B ase> f() {
std::auto_ptr<D erived> 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<D erived> derived(new Derived());
std::auto_ptr<B ase> 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

Jul 22 '05 #2
Siemel Naran wrote:
This code fails compile

std::auto_ptr<B ase> f() {
std::auto_ptr<D erived> 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<D erived> derived(new Derived());
std::auto_ptr<B ase> 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<Derive d> d(new Derived());
return d;
}
---

-dr
Jul 22 '05 #3

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

Similar topics

3
2343
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T> refcount { ... // reference counting wrapper; like shared_ptr }; class foo {
2
4589
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the following (in file testInteger.cc): line 38: std::auto_ptr<HInteger> d(new HInteger(5)); line 39: std::auto_ptr<HObject> e(new HObject());
0
9956
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10429
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4627
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.