473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

implicit conversion through constructor

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
3 2443
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
25
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x;...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
2
by: tim | last post by:
We've started to use a coding standards checker at work. The following code results in a warning: 38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool bSaveLast) 39: ...
1
by: flopbucket | last post by:
Hi, If I have: template<class T> class X { ..... };
3
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.