473,888 Members | 1,603 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 6152
"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
3960
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
1778
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
1897
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
10029
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
13288
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
10998
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
3311
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
3108
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
3709
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
9961
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
9800
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
11178
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
10777
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...
0
9597
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...
1
7990
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
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4243
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.