By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

implicit conversion through constructor

P: n/a
Hello,

I can perform implicit conversion through constructor, like

class A{
public:
A(int x):a(x){};
int a;
};

int main(){
A a = 10;
return 0;
}

However, if I have

class A{
public:
A(int x, int y):a(x),b(y){};
int a;
int b;
};

Then the compiler doesn't allow me to convert:

int main(){
A a = (10,20);
return 0;
}

How can I perform implicit conversion through constructor with
multiple arguments?

Many thanks,
Jess

Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jess <wd***@hotmail.comwrote:
However, if I have

class A{
public:
A(int x, int y):a(x),b(y){};
int a;
int b;
};

Then the compiler doesn't allow me to convert:

int main(){
A a = (10,20);
return 0;
}

How can I perform implicit conversion through constructor with
multiple arguments?
You can not. If the constructor needs more than one argument, you must
be explicit, like:

A a = A(10, 20);

or

A a(10, 20);

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 14 '07 #2

P: n/a
On 14 Jun, 14:36, Jess <w...@hotmail.comwrote:
Hello,

I can perform implicit conversion through constructor, like

class A{
public:
A(int x):a(x){};
int a;

};

int main(){
A a = 10;
return 0;

}
Your constructor with the signature A(int x) is saying "an object of
type A can be implicitly created from an object or expression of type
int". Implicit conversions from one type to another exist for the
built-in types in the language, and this feature allows you to create
your own types that have the same behaviour.
However, if I have

class A{
public:
A(int x, int y):a(x),b(y){};
int a;
int b;

};

Then the compiler doesn't allow me to convert:

int main(){
A a = (10,20);
This statement tries to use the expression (10,20) to initialise a.
The expression (10,20) has the type int and evaluates to the value 20
(the comma operator evaluates its first argument, 10, and discards it
then evaluates its second argument, 20, and returns that value. The
statement won't compile and the error you get should say something
along the lines of "no constructor available that takes a single int".

Try this code with your original class A definition where the
constructor was A(int x) and it will compile.
return 0;

}

How can I perform implicit conversion through constructor with
multiple arguments?
You can't. Implicit conversion is about converting from one type to
another type. If you have multiple arguments to the constructor there
is no one type to implicitly convert from.

Gavin Deane

Jun 14 '07 #3

P: n/a
Jess wrote:
Hello,

I can perform implicit conversion through constructor, like

class A{
public:
A(int x):a(x){};
int a;
};

int main(){
A a = 10;
return 0;
}

However, if I have

class A{
public:
A(int x, int y):a(x),b(y){};
int a;
int b;
};

Then the compiler doesn't allow me to convert:

int main(){
A a = (10,20);
First of all, this should mean that the language would need some notion of
tuples. C++ doesn't support that. This syntax, however would use the comma
operator on 10 and 20, which throws away 10 and returns 20.

This example is rather uninteresting, too.

A a /*=*/ (10, 20);

would compile, and has almost the same syntax.
return 0;
}

How can I perform implicit conversion through constructor with
multiple arguments?
You can't. And why would you? The meaning of implicit is that it is
implicit, and even if you want some syntactic sugar, your example would be
mostly explicit. Look at this:

void foo(A);
....
int value = some expression;
foo(value);

Here, the argument value is implicitly converted to an A object (assuming
the first definition of A). Should your constructor mean that

foo(value,value);

would call the same function with both arguments converted to (assuming the
latter A) A? Or would you have to write foo((value, value)), which would be
more explicit.

If you want to pass more than one value per parameter, or return more than
one value, you should look at the tuple library from boost.org.

--
rbh
Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.