Reg. For copy constructor.

S

skishorev

Hi,

I know What is the copy constructor. I don't know where and why we have
to use copy constructor. If You know please give to me with a situation
where we have to use or with a small example.
I am waiting for your reply.

Thanks & Regards,
Sai Kishore
 
F

flopbucket

The copy constructor is used whenever an object needs to be copied,
i.e.:

class foo { ... }

main() {
foo x;

func(x);
}


func(foo y)
{
....
}

when you call func and pass your object x, the copy constructor is
called to make y in the foo method.

If you dont supply a copy constructor, the compiler will make its own,
but it may or may not do what you really need (for example, if your
class holds pointers, etc).
 
S

skishorev

Hello,

Yes, I understood some thing. But, I didn't got complete idea.Here,
pssing a value from the function , then copy constructor will called or
compiler will generate default copy constructor .
Is there any other situation where the copy constructor will be called?
Suppose If i never supply a copy constructor , Then at every time
compiler only will generate a copy constructor?
I am very thankful to Your valuable response.
 
R

Rolf Magnus

Yes, I understood some thing.

Of what? Please quote context.
But, I didn't got complete idea.Here, pssing a value from the function ,
then copy constructor will called or compiler will generate default copy
constructor .
Is there any other situation where the copy constructor will be called?

The copy constructor will be used whenever you copy an object.
Suppose If i never supply a copy constructor , Then at every time
compiler only will generate a copy constructor?

Not sure what you mean by "every time". If you mean that every class that
you create without a user-defined copy constructor will get a
compiler-generated one, then you're right.
 
A

Avi

Whenever a object needs to be copied, copy constructor is called. There
are three situations where copy constructor is called.....
(1) Explicitly:
Suppose there is a class ABC, then in the following cases at 2nd
line of code, copy constructor is called.
ABC a1;
ABC a2 = a1; ...... 2nd line
(2) When a object is passed by value to called function.
(3) When a object is returned by value from called function.

Thanks,
 
J

Jarmo Muukka

Avi said:
Whenever a object needs to be copied, copy constructor is called. There
are three situations where copy constructor is called.....
(1) Explicitly:
Suppose there is a class ABC, then in the following cases at 2nd
line of code, copy constructor is called.
ABC a1;
ABC a2 = a1; ...... 2nd line
(2) When a object is passed by value to called function.
(3) When a object is returned by value from called function.

Thanks,

(1)
ABC a2 = a1;

may seem to require operator=( const ABC& ), but it does not. It uses
copy-ctor.

(2)
void fun( ABC a ) // copy-ctor is used
{
}

void main()
{
ABC x;
fun( x );
}

(3)
ABC fun() // copy-ctor is used
{
ABC a;
return a;
}

void main()
{
ABC x = fun();
}

If you do not supply copy-ctor, compiler will generate one. This will copy
each byte in source to destionation, so it will cause problems. Because of
this, I always write following line in my class, even I do not implement it.
It avoids compiler generated copy-ctor and you will get compiler error if
you try to use copy-ctor.

private:
ABC( const ABC& source );
ABC& operator=( const ABC& source );

Compiler will generate assignment operator too, if you do not supply one! In
the case above, I have supplied it, but with missing implementation.

(2)
You can avoid copy-ctor, if fou declare it in this way. It's faster too.

void fun( const ABC& a )
{
}

const says to the caller that fun does not modify the object.

HTH,
JMu
 
J

Jonathan Mcdougall

Jarmo said:
(1)
ABC a2 = a1;

may seem to require operator=( const ABC& ), but it does not. It uses
copy-ctor.

(2)
void fun( ABC a ) // copy-ctor is used
{
}

void main()

main() returns an int, always.
{
ABC x;
fun( x );
}

(3)
ABC fun() // copy-ctor is used
{
ABC a;
return a;
}

void main()
idem.

{
ABC x = fun();
}

If you do not supply copy-ctor, compiler will generate one. This will copy
each byte in source to destionation,

No, it will call operator= for every member.
so it will cause problems. Because of
this, I always write following line in my class, even I do not implement it.
It avoids compiler generated copy-ctor and you will get compiler error if
you try to use copy-ctor.

private:
ABC( const ABC& source );
ABC& operator=( const ABC& source );

Compiler will generate assignment operator too, if you do not supply one! In
the case above, I have supplied it, but with missing implementation.

This is useful if you class does not support copy or if it is not yet
implemented. This will catch errors at link time. However, if your
class may be copied and the generated copy constructor or assignment
operator are okay, just let the compiler do its job.


Jonathan
 
G

Gavin Deane

Jonathan said:
No, it will call operator= for every member.

For a compiler generated copy constructor?

The compiler generated copy constructor will call the copy constructor
for every base and member. The compiler generated assignment operator
will call the assignment operator, that is operator=, for every base
and member.

However, if your
class may be copied and the generated copy constructor or assignment
operator are okay, just let the compiler do its job.

Definitely.

Gavin Deane
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top