implicit conversion through constructor

J

Jess

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
 
M

Marcus Kwok

Jess said:
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);
 
G

Gavin Deane

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
 
R

Robert Bauck Hamar

Jess said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top