473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ctor ambiguity

I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(doub le r, double i = 10.0) : re(r), im(i) { }
my_complex(doub le r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.

Apr 19 '07 #1
3 1640
* su************* *@yahoo.com, India:
I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(doub le r, double i = 10.0) : re(r), im(i) { }
my_complex(doub le r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.
Decide whether you want the default for im to be 0.0 or 10.0.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ?
There's no ambiguity if you're not trying to call one of those constructors.

The ambiguity is not caused by creating an object: you can define some
other constructor and create an object using that constructor; e.g., you
should be able to do

my_complex heh = heh;

using the automatically generated copy constructor (which is an
interesting case of circumventing the C++ initialization guarantee).

--
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?
Apr 19 '07 #2
su************* *@yahoo.com, India wrote:
However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.
Oh, well, strictly speaking there is no ambiguity as soon as you ask for
a function that has multiple candidates. I mean, if you create an object

my_complex c(1.0,1.0);

it should be fine. You should wonder why the compiler shouldn't warn you
about the fact that you can't solve any more the function call if you
give just one argument.

There is indeed a general way to solve the ambiguity: consider this
simpler code:

int f()
{
return 0;
}

int f(int i = 0)
{
return i;
}

int main()
{
int (*fp)(void) = f;
fp();
return 0;
}
Bye!

Zeppe
Apr 19 '07 #3
su************* *@yahoo.com, India wrote:
I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(doub le r, double i = 10.0) : re(r), im(i) { }
my_complex(doub le r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.
There's no ambiguity in the case of:
my_complex(5.0, 5.0)
for example.

The compiler might notice that the one arg'd constructor can't be
unambiguously reached, but it's not required, and frankly if your
program never creates an object like my_complex(100. 0), it would
not be an issue.
Apr 19 '07 #4

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

Similar topics

10
2124
by: Momchil Velikov | last post by:
The following program produces error at the definition of ``b''. However it compiles fine the second (supposedly equivalent) form of the templated constructor. Why the two alternatives aren't equivalent ? template<class Obj> struct ptr_to_member { typedef void (Obj::* type) (); };
1
1861
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class my_template
10
1359
by: richardclay09 | last post by:
The output of this: #include <iostream> #include <vector> using namespace std; struct X { int i; X(const X& x) : i(x.i) { cout << "ctor copy: " << i << endl;
5
3025
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
8
2736
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/cb014f4ba9df614a Can anybody answer? Who can't read in russian:
5
1799
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
3
3397
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
2
2164
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide the default ctor. Suppose we try to create Test obj;
4
3374
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
8674
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
9157
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...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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
6518
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
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3046
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
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.