473,569 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2042
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
10551
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 equivalent to executing the default copy constructor (i.e. lines 33)? (b) If not, is the behavior for lines 36-39 well defined by the standard?
12
2664
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 exception instead. If I step into __CxxThrowException in debug/assembly mode, it looks like the pThrowInfo parameter created by the compiler excluded...
26
15778
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
2608
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 section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
13
4992
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 to make it more manageable. Below is a draft of a document that I plan to give to my introductory C++ class. Please note that I have purposely...
2
1335
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 (incomplete/illustrative) code: class A { // (copy) ctors goe here... };
6
1555
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 appears to think that it actually calls one of the *other* constructors. The copy ctor would work in this context, but the other ctor can't be used...
3
3894
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* foo; which a complicated code allocates as one of derived's and sets up.
9
2305
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
7926
Oralloy
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. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
tracyyun
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...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
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...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
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...

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.