473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

default and copy ctors

If we provide any ctor for a class the compiler does not supply the
default ctor.
However if we do not provide the copy ctor but provide any other ctor,
the compiler still supplies the copy ctor.

Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?

Kindly explain

Thanks
V.Subramanian

Jun 26 '07 #1
3 2023
su**************@yahoo.com, India wrote:
If we provide any ctor for a class the compiler does not supply the
default ctor.
However if we do not provide the copy ctor but provide any other ctor,
the compiler still supplies the copy ctor.

Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?
If you provide your own version of a constructor, you have some special
functionality in mind to bring your newly constructed object into a well-defined
state, so as to fulfill some class invariant. Obviously, the compiler generated
default constructor cannot guess this functionality, so it is discarded. As you
stated, this applies only to the default constructor. The rationale behind this
may be, that copying may still be a simple task (copying the object bit for bit
could preserve the class invariant), so even if the standard constructor doesn't
meet your requirements, the copy constructor still may do so.

If you ask me, I'd say that C++ should have some features to describe whether a
compiler generated piece of functionality is desired:

class SomeClass
{
// This could be a way to specify that the default standard constructor
// should be generated.
using SomeClass ();

// This could be a way to specify that the default copy constructor
// should be generated.
using SomeClass (const SomeClass& other);
};

Regards,
Stuart
Jun 26 '07 #2
On 2007-06-26 07:26, su**************@yahoo.com, India wrote:
If we provide any ctor for a class the compiler does not supply the
default ctor.
However if we do not provide the copy ctor but provide any other ctor,
the compiler still supplies the copy ctor.

Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?

Kindly explain
Because usually you want a copy ctor, and with the behaviour of the
default one. It's quite usual that you need to supply your own ctor but
that the default copy ctor is good enough so it simply saves time, same
as the fact that you get a default operator=, it would take too much
time if you'd have to write them all the time. Consider the case of a
simple struct:

struct Test {
int foo;
int bar;
double baz;
};

It might be nice to have a ctor so you can create and initialize a new
instance at the same time, but you probably don't want to change the
behaviour of the default copy ctor or operator=.

--
Erik Wikström
Jun 26 '07 #3
On 2007-06-26 09:21, Stuart Redmann wrote:
su**************@yahoo.com, India wrote:
>If we provide any ctor for a class the compiler does not supply the
default ctor.
However if we do not provide the copy ctor but provide any other ctor,
the compiler still supplies the copy ctor.

Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?

If you provide your own version of a constructor, you have some special
functionality in mind to bring your newly constructed object into a well-defined
state, so as to fulfill some class invariant. Obviously, the compiler generated
default constructor cannot guess this functionality, so it is discarded. As you
stated, this applies only to the default constructor. The rationale behind this
may be, that copying may still be a simple task (copying the object bit for bit
could preserve the class invariant), so even if the standard constructor doesn't
meet your requirements, the copy constructor still may do so.

If you ask me, I'd say that C++ should have some features to describe whether a
compiler generated piece of functionality is desired:

class SomeClass
{
// This could be a way to specify that the default standard constructor
// should be generated.
using SomeClass ();

// This could be a way to specify that the default copy constructor
// should be generated.
using SomeClass (const SomeClass& other);
};
In a way you can get this functionality, except you have to specify
which ones you don't want instead of which you want. Just declare any
you don't want private.

--
Erik Wikström
Jun 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: jonathan cano | last post by:
QUESTION: In practice, lines 36 and 37 below are usually equivalent to the default copy constructor (.e.g line 33). My questions are: (a) Does ISO 14882 guarantee that lines 36 and 37 are...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
2
by: Jeroen | last post by:
Hi all, I wrote a little bit of code to see the behaviour of (copy) constructors of base and derived classes. But I have a question about it. Let me explain by the following...
6
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler...
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
9
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.