473,662 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy ctor vs default ctor

If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.

Kindly explain

Thanks
V.Subramanian

Aug 15 '07 #1
2 2161
su************* *@yahoo.com, India wrote:
If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.
The C++ standard specifies it that way. I can only imagine that the C++
standard was specified this way because it's more likely that if a
default CTOR is not provided while others are, you more than likely
don't want a default ctor while you do want a copy ctor.
Aug 15 '07 #2
On 2007-08-15 08:55, su************* *@yahoo.com, India wrote:
If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.
Because the generation of default ctor and copy ctor are independent of
each other. If you declare any ctor (even a copy ctor) the compiler wont
generate a default ctor. If you declare a copy ctor the compiler wont
generate a default copy ctor, nor will it generate a default ctor.

--
Erik Wikström
Aug 15 '07 #3

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

Similar topics

3
4461
by: mescaline | last post by:
//Consider the simple program with inheritance, plain init for A, copy ctr for B #include <iostream> using namespace std; class Base{ public: Base(){cout << "Base, default" << endl;} Base(const Base &){cout << "Base, Copy Ctor" << endl;} };
8
449
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make copies of classes with members with static or dynamic memory allocation. However to do this it requires the programmer to override the default copy constructor, the destructor & operator=. I wrote a small program to test if i can doa deep copy...
5
1692
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. My observation: 1. Compiler does not complain.
9
1670
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
2118
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a const member (since it cannot be made to reference any other object once it has been initialized). ...
10
4015
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
3
2047
by: subramanian100in | last post by:
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
1
1050
by: Subramanian India | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide the default ctor. Suppose we try to create Test obj; Here the default ctor is needed and the compiler does not provide the default ctor but generates a compilation error.
9
2887
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no Foo() is included, i believe. };
0
8435
marktang
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...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5655
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
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
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.