473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

base/derived ambiguity with gcc, but not with MSVC

When I compile the below program with Microsoft Visual C++ version 6, I
get the results I expect, which is that the program should write out

base()
derived()
base()
derived(derived)

When I try to compile it with gcc, I instead get the compiler error message

test_prog.cpp: In function `int main(int, char**)':
test_prog.cpp:43: call of overloaded `derived(derived_handle&)' is ambiguous
test_prog.cpp:18: candidates are: derived::derived(const base&)
test_prog.cpp:17: derived::derived(const derived&)

Why is this?

The statement that causes the problem is

derived d(my_dhandle); // <-- Ambiguous according to gcc

but I really can't understand why it should be considered ambiguous.

The class 'derived' has both a copy constructor and a constructor that
takes a 'base' object as input, and the handle 'my_dhandle' has an
automatic conversion to 'derived&', so I would have expected the
compiler to prefer the constructor that is the closest match for the
actual parameter, and pick the constructor 'derived(const derived&)'
over 'derived(const base&)'.

But it doesn't, so evidently I've missed something. But what?

(ps. I'm aware that the test program as it is written here would leak
memory, but that's not the problem right now, since I can't even get it
to compile.)
================================================== ============================
test_prog.cpp:

#include <iostream>

//----------------------------------------------------------------------------
class base
{
public:
base() {std::cout << "base()" << std::endl;}
base(const base&) {std::cout << "base(base)" << std::endl;}
};
//----------------------------------------------------------------------------
class derived : public base
{
public:
derived() {std::cout << "derived()" << std::endl;}
derived(const derived&) {std::cout << "derived(derived)" <<
std::endl;}
explicit derived(const base&) {std::cout << "derived(base)" <<
std::endl;}
};
//----------------------------------------------------------------------------
class derived_handle
{
derived* tptr;

public:
derived_handle() {};
derived_handle(derived* inptr) : tptr(inptr) {}

derived& operator*() const {return *tptr;}
derived* operator->() const {return tptr;}

operator derived&() const {return **this;} // Implicit conversion
to 'derived'
};
//================================================== ==========================
int main (int argc, char* argv[])
{
derived_handle my_dhandle(new derived());

derived d(my_dhandle); // <-- Ambiguous according to gcc

return 0;
}

Jul 22 '05 #1
2 1966
On Wed, 11 Feb 2004 13:44:47 +0100, =?ISO-8859-1?Q?Christian_Engstr=F6m?= <ch****************@glindra.org> wrote:
When I compile the below program with Microsoft Visual C++ version 6, I
get the results I expect, which is that the program should write out

base()
derived()
base()
derived(derived)

When I try to compile it with gcc, I instead get the compiler error message

test_prog.cpp: In function `int main(int, char**)':
test_prog.cpp:43: call of overloaded `derived(derived_handle&)' is ambiguous
test_prog.cpp:18: candidates are: derived::derived(const base&)
test_prog.cpp:17: derived::derived(const derived&)

Why is this?


I get the above errors with g++ 3.2.3, but it compiles fine with g++ 3.3.

A compiler upgrade seems to be in order... ;-)

Jul 22 '05 #2
Alf P. Steinbach wrote:
I get the above errors with g++ 3.2.3, but it compiles fine with g++ 3.3.

A compiler upgrade seems to be in order... ;-)


Yes, that was it, it seems. Thanks!

Jul 22 '05 #3

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

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
2
by: dumboo | last post by:
hi there i m having a base class class base { ... }; and two derived class, which have inherited 'base' but the inheritance is not virtual
2
by: Siemel Naran | last post by:
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...
2
by: Alex Sedow | last post by:
Why C# lookup mechanism can't found indirect base class by name? It is seems very strange: derived class can access to base members, but not class itself. Example: namespace N1 { class B {...
4
by: Alex Sedow | last post by:
Method invocation will consist of the following steps: 1. Member lookup (14.3) - evaluate method group set (Base.f() and Derived.f()) .Standart say: "The compile-time processing of a method...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
1
by: PengYu.UT | last post by:
I try to invoke the operator of the base class. ** line doesn't work. But *** line works. However, *** lines becomes problematic with multiple inheritance is used for "derived". **** works, but...
3
by: Juha Nieminen | last post by:
It occurred to me while developing an application: What happens if two (completely independent) base classes have the exact same virtual function, and then a derived class is derived from both,...
14
by: Jo | last post by:
Hi, Is there a generic way to access the (virtual) functions in the base class of a class? Like in: if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for a normal member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.