Connecting Tech Pros Worldwide Help | Site Map

implicit constructor

Rahul
Guest
 
Posts: n/a
#1: Oct 30 '07
Hi Everyone,

I know that implicit conversion is invoked for the following case,

class A
{
public: A(int n)
{
...
}
};


A obj = 2;

But is any temp objects created? Is the 2 used to create a temp
object and then is the temp object assigned to the the actual object
obj?

Thanks in advance!!!

Victor Bazarov
Guest
 
Posts: n/a
#2: Oct 30 '07

re: implicit constructor


Rahul wrote:
Quote:
Hi Everyone,
>
I know that implicit conversion is invoked for the following case,
>
class A
{
public: A(int n)
{
...
}
};
>
>
A obj = 2;
>
But is any temp objects created? Is the 2 used to create a temp
object and then is the temp object assigned to the the actual object
obj?
The semantics are such that a temporary object is created. However,
the Standard allows the compiler to forgo creation of a temporary in
such case and construct 'obj' as if you wrote

A obj(2);

The copy-constructor in 'A' has to still be accessible, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Rolf Magnus
Guest
 
Posts: n/a
#3: Oct 30 '07

re: implicit constructor


Rahul wrote:
Quote:
Hi Everyone,
>
I know that implicit conversion is invoked for the following case,
>
class A
{
public: A(int n)
{
...
}
};
>
>
A obj = 2;
>
But is any temp objects created? Is the 2 used to create a temp
object and then is the temp object assigned to the the actual object
obj?
It's not assigned, but rather copy-constructed. Apart from that, see
Victor's answer.

Closed Thread