473,396 Members | 1,861 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,396 software developers and data experts.

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 2136
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
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;}...
8
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...
5
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. ...
9
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
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...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
3
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....
1
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...

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.