473,785 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to avoid ambiguous constructors?

I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?
Jul 22 '05 #1
10 6140
"Jason Heyes" <ge******@optus net.com.au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?


I just found one way to fix my ambiguous constructors. You need to add a
dummy int parameter to one of the constructors. When you want to call that
particular constructor, you specify a zero argument.
Jul 22 '05 #2
Jason Heyes wrote:
"Jason Heyes" <ge******@optus net.com.au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?


I just found one way to fix my ambiguous constructors. You need to add
a dummy int parameter to one of the constructors. When you want to
call that particular constructor, you specify a zero argument.


I'd rather use an enum to make the resulting code more readable:

class Length
{
public:
enum Unit { Centimeter, Inch };

Length(double l, Unit u = Centimeter);

//...
};

However, you will need to select (with if or switch/case) the code
depending on the value.
If you want two separate constructors, you can do it too with a small
modification:

class Length
{
public:
enum Centimeter_t { Centimeter };
enum Inch_t { Inch };

Length(double l, Centimeter_t = Centimeter);
Length(double l, Inch_t);

//...
};

Now you can write:

Length l1(3); // 3 centimeters, constructor 1
Length l2(5, Inch); // 5 inches, constructor 2
Length l3(7, Centimeter); // 7 centimeters, constructor 1

If you leave out the default value, you can enforce the explicit
specification of the unit, and the definition of l1 becomes invalid.

Jul 22 '05 #3

"Jason Heyes" <ge******@optus net.com.au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?


The solution should actually present itself if you decide how the
constructors are actually different. If you can't, then they are actually
the same. When you identify under what conditions you would want one rather
than the other, I think you'll find you can simply use the same one, but
send a parameter that is a flag or enum to tell the constructor what to do
differently when it is called with that parameter value. I doubt you really
need more than one constructor.
Jul 22 '05 #4
On Fri, 05 Dec 2003 22:10:12 +1100, Jason Heyes wrote:
"Jason Heyes" <ge******@optus net.com.au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?


I just found one way to fix my ambiguous constructors. You need to add a
dummy int parameter to one of the constructors. When you want to call that
particular constructor, you specify a zero argument.


Sounds like what you want to do is have an argument that represents a mode
of construction. Much the the fstream class takes an optional openmode as
an argument. So instead of having two constructors, you have one which
behaves differently based on the mode parameter.

Brad
Jul 22 '05 #5
On Fri, 5 Dec 2003 21:58:12 +1100, "Jason Heyes"
<ge******@optus net.com.au> wrote:
I have a class with two constructors that both take the same type of
argument as a parameter. What should I do to disambiguate my class?


you could also make your two constructors explicit:

class foo
{
explicit foo(int);
explicit foo(unsigned int);
};
Jul 22 '05 #6

"Dan W." <da**@raytron-controls.com> wrote in message news:0o******** *************** *********@4ax.c om...

you could also make your two constructors explicit:

class foo
{
explicit foo(int);
explicit foo(unsigned int);
};


How does this help? If it was ambiguious before it still is ambiguous.
i.e.
foo(1L); // still bad.
Jul 22 '05 #7
On Fri, 5 Dec 2003 15:49:32 -0500, "Ron Natalie" <ro*@sensor.com >
wrote:

"Dan W." <da**@raytron-controls.com> wrote in message news:0o******** *************** *********@4ax.c om...

you could also make your two constructors explicit:

class foo
{
explicit foo(int);
explicit foo(unsigned int);
};


How does this help? If it was ambiguious before it still is ambiguous.
i.e.
foo(1L); // still bad.


DOH ! I was thinking with the wrong body parts... ;-)

Jul 22 '05 #8
Dan W. wrote:
On Fri, 5 Dec 2003 15:49:32 -0500, "Ron Natalie" <ro*@sensor.com >
wrote:

"Dan W." <da**@raytron-controls.com> wrote in message
news:0o****** *************** ***********@4ax .com...

you could also make your two constructors explicit:

class foo
{
explicit foo(int);
explicit foo(unsigned int);
};


How does this help? If it was ambiguious before it still is
ambiguous. i.e.
foo(1L); // still bad.


DOH ! I was thinking with the wrong body parts... ;-)


Which body parts could be used for thinking about ambiguities?
Or was it the body part used to create new "objects"? ;-)

Jul 22 '05 #9
Jason Heyes wrote:
I have a class with two constructors
that both take the same type of argument as a parameter.
What should I do to disambiguate my class?


Do you really need all of these constructors?
You can define *pseudo instructors* instead:

class foo { // thanks to Dan W.
private:
// representation
int I;
public:
// constructors
foo(int);
// functions
friend
foo pseudo(int);
};
int main(int argc, char* argv[]) {
foo F(13);
foo G = pseudo(33);
return 0;
}

If you have a good optimizing C++ compiler,
the pseudo constructor won't cost any more
than an actual constructor.

Jul 22 '05 #10

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

Similar topics

7
3954
by: ishekara | last post by:
Hi, I am having a class template which is used to convert from one type another. I am having a problem when i use the copy constructor with same type. code. #include "stdio.h" template <class T> class SPtr {
16
1769
by: REH | last post by:
Can some tell me why the chooses the constructor in class B over operator B in class A? Is this not ambiguous? Thanks. #include <iostream> using namespace std;
14
1888
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public: A<T>& make_A() {return A<T>();}; };
1
10010
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
9
13278
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include <hash_map>//from Standard template library //and some other headers
5
10995
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These objects have constructors that allow implicit conversions from simple types. All this has been defined as follows: <code> class Int
3
3304
by: mast2as | last post by:
Hello, I have 2 classes *as an example* Point & Color and friend operator * in the class Point is defined in such a way that it is possible to do something like that Point p = Point( 1.0 ) * Color( 2.0 ); // in point.hpp class Color; class Point { public:
8
3103
by: xtrigger303 | last post by:
Hi to all, I'm working on a smart pointer implementation and I'm trying to get automatic type conversion between different pointer types. I stumbled upon something weird (at least for me) I summarized it in the code below. I was expecting both things at the end to work or not work at all.... Any insight? Thanks in advance, Francesco
34
3693
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But there is a constructor that accepts the base class. This one should be used for copying. In fact i have a set of classes with a common abstract base. The implementations do not have own data members. They only implement
0
9645
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
9480
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
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.