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 2568
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(enumCO PY, const Polyline&);
Polyline& become(enumCOPY , const Polyline&);
"lilburne" <li******@godzi lla.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******@godzi lla.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:
myBigExpensiveC lass& function(int);
myBigExpensiveC lass 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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
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 complain if the copy
constructor is private.
1) Is this part of the C++ language definition? What is the thinking
behind it?
2) In any case, how can I catch at compile time any callers that try to
|
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. Copy constructor is
called when we pass the argument by value and we return from the
function and also we initialize.
In the main function, I check the number of time the copy constructor
is called, I expect it to be 3
(1) parameter passing in...
|
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
assignment, should copy constructor and assignment operator be unnecessary?
2. If a base class have a pointer member, should its derived classes also
need copy constructor and assignment operator, no matter if a derived class
itself has a...
|
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 the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy...
| |
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>
class Vector {};
template<class Ref>
class VectorView: public Vector<VectorView<Ref> >
{
|
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 std;
class var_array
{
private:
int *data; // The data
|
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 sufficient?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |