We know that copy constructor is used if a class object is passed by
value to a function, is there any logic behind the use of copy
constructor as assignment operator could also have been used instead
of copy constructor
Regards
Madhukar 7 2550
madhukar_bm wrote: We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used instead of copy constructor
You first have to have an object to assign to it, the copy constructor
creates one.
madhukar_bm wrote: We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used instead of copy constructor
What would it assign to? You have to create a new object that is a copy
of an existing one, and that's exactly what the copy constructor is
for. The assignment operator is for making an existing object a copy of
another one.
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com... madhukar_bm wrote:
We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used instead of copy constructor
What would it assign to? You have to create a new object that is a copy of an existing one, and that's exactly what the copy constructor is for. The assignment operator is for making an existing object a copy of another one.
This is in fact an interesting question. If copy constructor is
inaccessible, is the system allowed to default-construct, and
then assign, to simulate the effect of copy construction when
it implicitly needs to? If not, would it make sense to allow it
to happen?
- Risto -
Risto Lankinen wrote: "Rolf Magnus" <ra******@t-online.de> wrote in message news:bv*************@news.t-online.com... What would it assign to? You have to create a new object that is a copy of an existing one, and that's exactly what the copy constructor is for. The assignment operator is for making an existing object a copy of another one.
This is in fact an interesting question. If copy constructor is inaccessible, is the system allowed to default-construct, and then assign, to simulate the effect of copy construction when it implicitly needs to? If not, would it make sense to allow it to happen?
NO! NO! NO! NO! NO!
In most of my class I specifically declare the copy ctor and assignment
operator as private, because I do not want anyone EVER writing code that
can create temporary copies.
so in our libraries what would normally be
Polyline(const Polyline&);
Polyline& operator=(const Polyline&);
are declared as:
Polyline(enumCOPY, const Polyline&);
Polyline& become(enumCOPY, const Polyline&);
"lilburne" <li******@godzilla.com> wrote in message
news:bv*************@ID-179504.news.uni-berlin.de... Risto Lankinen wrote:
This is in fact an interesting question. If copy constructor is inaccessible, is the system allowed to default-construct, and then assign, to simulate the effect of copy construction when it implicitly needs to? If not, would it make sense to allow it to happen?
NO! NO! NO! NO! NO!
In most of my class I specifically declare the copy ctor and assignment operator as private, because I do not want anyone EVER writing code that can create temporary copies.
Nothing to NO! about, since that would still be prevented
as long as assignment operator is private as well.
- Risto -
Risto Lankinen wrote: "lilburne" <li******@godzilla.com> wrote in message news:bv*************@ID-179504.news.uni-berlin.de...
Risto Lankinen wrote:
This is in fact an interesting question. If copy constructor is inaccessible, is the system allowed to default-construct, and then assign, to simulate the effect of copy construction when it implicitly needs to? If not, would it make sense to allow it to happen?
NO! NO! NO! NO! NO!
In most of my class I specifically declare the copy ctor and assignment operator as private, because I do not want anyone EVER writing code that can create temporary copies.
Nothing to NO! about, since that would still be prevented as long as assignment operator is private as well.
Well that does force you to make both private, and making each of the
functions private is done for seperate though related reasons, which
probably means there are times when you do want one without the other.
The private copy-ctor stops people from writing functions like:
void function();
I had a junior colleague come ask me just last week why it was that he
was getting compiler errors in the above case.
The private assignment operator stops people from writing:
myBigExpensiveClass& function(int);
myBigExpensiveClass x;
x = function(10);
Risto Lankinen wrote: "Rolf Magnus" <ra******@t-online.de> wrote in message news:bv*************@news.t-online.com... madhukar_bm wrote:
We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used instead of copy constructor
What would it assign to? You have to create a new object that is a copy of an existing one, and that's exactly what the copy constructor is for. The assignment operator is for making an existing object a copy of another one.
This is in fact an interesting question. If copy constructor is inaccessible, is the system allowed to default-construct, and then assign, to simulate the effect of copy construction when it implicitly needs to? If not, would it make sense to allow it to happen?
I opt for no.
If the copy ctor is inaccessible, there is a reason to it
(whatever that reason might be). To allow the compiler to find
a way around that, would circumvent that reason.
(There are already enough problems when the compiler tries
to use conversion operators and constructors to make things
compilable).
--
Karl Heinz Buchegger kb******@gascad.at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Edward Diener |
last post by:
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same...
|
by: Sam Wilson [Bentley] |
last post by:
If you pass a C++ object by value as an argument to a function which has
a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object's copy constructor and will not...
|
by: Vivek Shah |
last post by:
Hi,
Given below is apiece of code which I was writing to clear my concepts
of copy constructor. I have a function f() which takes Class A object
through call by value and return the same object....
|
by: away |
last post by:
1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.
If don't pass objects of such class as value in a function and don't do...
|
by: trying_to_learn |
last post by:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling...
|
by: Tony Johansson |
last post by:
Hello experts!
I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
|
by: Alexander Stippler |
last post by:
The following code snippet (those of you who read my last postings
might be familiar with it ;-)):
//--------------------------------------------------------------------
template<class Impl>...
|
by: pragtideep |
last post by:
Kindly help me explain the behaviour of defult copy constructor . Why
the destructor is freeing the SAME memory twice , though it was
allocated just once .
#include<iostream>
using namespace...
|
by: vishnupriya.sureshbabu |
last post by:
Why is it necessary to pass reference of the object in copy
constructor? What will happen if we pass by value? We are going to just
copy the values and not modify. So wont passing by value be...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |