Connecting Tech Pros Worldwide Help | Site Map

which constructor is used in the direct-initialization?

  #1  
Old December 7th, 2006, 02:35 AM
heng
Guest
 
Posts: n/a
class A
{
public:
int x;
A(int x_=0):x(x_){}
};

int main()
{
A obj1(99); //user-defined constructor is used
A obj2=obj1; //compiler-defined copy constructor is used
A obj3(obj1); //which constructor is used ? I am surprised that
this kind of direct-initialization
//works

Thanks for your kind help !

  #2  
Old December 7th, 2006, 02:55 AM
Salt_Peter
Guest
 
Posts: n/a

re: which constructor is used in the direct-initialization?



heng wrote:
Quote:
class A
{
public:
int x;
A(int x_=0):x(x_){}
};
Why not test it?

#include <iostream>

class A {
int x;
public:
A( int n = 0 ) : x(n) { std::cout << "A(int)\n"; }
A( const A& copy )
{
std::cout << "A copy\n";
x = copy.x;
}
};
Quote:
>
int main()
{
A obj1(99); //user-defined constructor is used
A obj2=obj1; //compiler-defined copy constructor is used
A obj3(obj1); //which constructor is used ? I am surprised that
this kind of direct-initialization
//works
>
Thanks for your kind help !
There is absolutely no difference between the following 2 statements:

A obj2 = obj1;
A obj2( obj1 );

  #3  
Old December 7th, 2006, 03:05 AM
heng
Guest
 
Posts: n/a

re: which constructor is used in the direct-initialization?


Salt_Peter wrote:


Thanks. But you explicitly define the copy constructor in your example,
I mean, without the explicit copy constructor, which constructor is
used in the expression like

A obj2(obj1);

is it still the copy constructor?

  #4  
Old December 7th, 2006, 03:15 AM
Victor Bazarov
Guest
 
Posts: n/a

re: which constructor is used in the direct-initialization?


heng wrote:
Quote:
Salt_Peter wrote:
>
>
Thanks. But you explicitly define the copy constructor in your
example, I mean, without the explicit copy constructor, which
constructor is used in the expression like
>
A obj2(obj1);
>
is it still the copy constructor?
Probably. If you don't declare one, it will be declared for you,
and defined (if possible).

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


  #5  
Old December 7th, 2006, 03:55 AM
Salt_Peter
Guest
 
Posts: n/a

re: which constructor is used in the direct-initialization?



heng wrote:
Quote:
Salt_Peter wrote:
>
>
Thanks. But you explicitly define the copy constructor in your example,
I mean, without the explicit copy constructor, which constructor is
used in the expression like
>
A obj2(obj1);
>
is it still the copy constructor?
Yes, the compiler must provide a copy ctor unless you explicitly
prevent it.
Otherwise, the above would generate an error.
So why not implement the copy to match your needs?

To push the logic a little further, how come the following works?

class A {
};

int main()
{
A a; // def ctor
A b(a); // copy ctor
a = b; // assignment - not a copy!!!
}

  #6  
Old December 7th, 2006, 04:25 AM
Ivan Novick
Guest
 
Posts: n/a

re: which constructor is used in the direct-initialization?


heng wrote:
Quote:
class A
{
public:
int x;
A(int x_=0):x(x_){}
};
>
int main()
{
A obj1(99); //user-defined constructor is used
A obj2=obj1; //compiler-defined copy constructor is used
A obj3(obj1); //which constructor is used ? I am surprised that
this kind of direct-initialization
//works
>
Thanks for your kind help !
That is part of the definition of the language. If you do not define
your own copy constructor the compiler will create an implicit one for
you. The compiler created copy constructor performs a memberwise copy
of its subobjects.
-
Ivan
http://www.0x4849.net

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoid automatic copy constructor generation =?ISO-8859-1?Q?Marcel_M=FCller?= answers 34 October 28th, 2008 11:55 PM
explicit call of constructor and destructor Rahul answers 12 December 5th, 2007 03:25 PM
iexplorer halts on exit when ActiveXObject has been used Simon answers 9 January 11th, 2006 07:35 PM
constructor slurper answers 24 July 22nd, 2005 11:15 PM
Overloading new and delete in C++. answers 5 July 22nd, 2005 07:40 AM