Copy Constructor with Pointer instead of Reference

M

Mohan

Hi,

What are the advantages/disadvantages of using a pointer instead of
Reference in the Copy Constructor ?

For Example,

Writing the Copy constructor for the Class "Temp" as below,

Temp(const base *ptrBase)
{
}

instead of
Temp(const base &objBase)
{
}


Thanks & Regards,
Mohan
 
A

Alf P. Steinbach

* Mohan:
> What are the advantages/disadvantages of using a pointer instead of
Reference in the Copy Constructor ?

For Example,

Writing the Copy constructor for the Class "Temp" as below,

Temp(const base *ptrBase)
{
}

instead of
Temp(const base &objBase)
{
}

None of these are copy constructors.

Consult your nearest C++ textbook to learn what a copy constructor is.
 
D

dim.kailash

In case of copy constructor if we use donot use reference in that case
it will create an object and call the copy constructor and passing the
value to the copy constructor and each time a new object is created and
each time it will call the copy constructor it goes to infinite and
fill the memory then it display the error message .


if we pass the reference it will not create the new object for storing
the value. and no recursion will take place
 
M

Mohan

Thanks for the reply,

I am sorry, it was a copy/paste problem

Temp(const Temp *ptrBase)
{
}

My intention is to know will there be performace or any other issues ??
 
M

Mohan

Thanks for the reply,

I am sorry, it was a copy/paste problem

Temp(const Temp *ptrBase)
{
}

My intention is to know will there be performace or any other issues in
using a Ponter instead of reference in Copy constructor??
 
M

Mohan

Thanks for the reply,

I am sorry, it was a copy/paste problem

Temp(const Temp *ptrBase)
{
}

My intention is to know will there be performace or any other issues in
using a Ponter instead of reference in Copy constructor??
 
A

Alf P. Steinbach

* Mohan:
Thanks for the reply,

I am sorry, it was a copy/paste problem

Temp(const Temp *ptrBase)
{
}

My intention is to know will there be performace or any other issues ??

Please don't top-post. Please don't quote signatures. Please see my
original answer quoted somewhere above (it still applies, after the code
correction).
 
D

David W

Mohan said:
Thanks for the reply,

I am sorry, it was a copy/paste problem

Temp(const Temp *ptrBase)
{
}

My intention is to know will there be performace or any other issues ??

On performance, it's up to the machine and the compiler, but very likely there would be no
difference at all. On other issues, a reference would be more convenient than a pointer in most
cases, and absolutely essential in others, since your pointer version is not a copy constructor.
Whether to copy from a pointer or a reference just isn't an issue. AFAIK, everyone uses a reference
without even thinking.

DW
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top