473,651 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy ctor

Consider the code fragment:

class Test {
public:
Test(const Test &temp);
...
};

....

In the copy ctor, we put "const". I understand that by putting const,
we do not intend to modify the parameter. Is there any other reason for
putting const in the copy ctor ?

Jan 7 '07 #1
3 2378
* subramanian:
>
In the copy ctor, we put "const". I understand that by putting const,
we do not intend to modify the parameter. Is there any other reason for
putting const in the copy ctor ?
With the current standard it allows you to copy a temporary, since a
temporary can be bound to a reference to const, but not to a reference
to non-const.

Note, by the way, that there are four possible copy constructors for a
class T, only two of which has reference to const argument:

T( T& );
T( T const& );
T( T volatile& );
T( T const volatile& );

AFAIK there isn't any reason, ever, to use 'volatile' here; it's just a
consequence of an attempt to unify the rules for 'const' and 'volatile'
qualifications, so-called "cv qualification".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 7 '07 #2
Alf P. Steinbach wrote:
* subramanian:
>>
In the copy ctor, we put "const". I understand that by putting const,
we do not intend to modify the parameter. Is there any other reason for
putting const in the copy ctor ?

With the current standard it allows you to copy a temporary, since a
temporary can be bound to a reference to const, but not to a reference
to non-const.

Note, by the way, that there are four possible copy constructors for a
class T, only two of which has reference to const argument:

T( T& );
T( T const& );
T( T volatile& );
T( T const volatile& );
Actually, there is an infinite number of possible copy constructors. A
constructor with more than one argument can still be a copy constructor if
all the following arguments have default values.
The standard (at lest the 98 version) also seems inconsistent about what is
allowed for the first parameter to make it a copy constructor. §12.1/10
only mentions T& and const T&, while 12.8 also mentions the versions with
volatile.

Jan 7 '07 #3
* Rolf Magnus:
Alf P. Steinbach wrote:
>* subramanian:
>>In the copy ctor, we put "const". I understand that by putting const,
we do not intend to modify the parameter. Is there any other reason for
putting const in the copy ctor ?
With the current standard it allows you to copy a temporary, since a
temporary can be bound to a reference to const, but not to a reference
to non-const.

Note, by the way, that there are four possible copy constructors for a
class T, only two of which has reference to const argument:

T( T& );
T( T const& );
T( T volatile& );
T( T const volatile& );

Actually, there is an infinite number of possible copy constructors. A
constructor with more than one argument can still be a copy constructor if
all the following arguments have default values.
The standard (at lest the 98 version) also seems inconsistent about what is
allowed for the first parameter to make it a copy constructor. §12.1/10
only mentions T& and const T&, while 12.8 also mentions the versions with
volatile.
Yes. I once suggested in [comp.std.c++] to make the former para just
refer to the latter. There's also the question of tidying up the
wording for consistency, i.e. same kind of wording for default
constructor and copy constructor, and possibly elsewhere, especially
since the different wording might seem to allow T(...) as default
constructor but not T(T const&,...) as copy constructor. E.g. consider

struct T
{
T(){};
T(T const&, ...){}
};

int main()
{
T x(( T() ));
}

It might seem that the second constructor isn't a copy constructor, but
in fact it has only one parameter: the ellipsis is not regarded as a
parameter (it all hinges on the meaning of "parameter" , whereas that's
not a problem with the wording adopted for the default constructor).

Everybody agreed that some cleaning up of the wording would be a good
idea, but AFAIK nothing more came out of it.

The exclusion of templated copy constructors in §12.1/10 is also problem
wrt. to what "copy constructor" really means in the standard.

Perhaps that's what stands in the way, that the whole thing is a bit
complicated.

Let's just hope the OP is not now hopelessly confused... <g>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 7 '07 #4

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

Similar topics

11
2325
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
7
2677
by: skishorev | last post by:
Hi, I know What is the copy constructor. I don't know where and why we have to use copy constructor. If You know please give to me with a situation where we have to use or with a small example. I am waiting for your reply. Thanks & Regards, Sai Kishore
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.
1
2117
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). ...
3
3388
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
10
4013
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
13
2461
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
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
2
2160
by: subramanian100in | 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;
11
2278
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
8357
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
8277
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,...
1
8465
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,...
0
8581
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
7298
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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
4144
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...
2
1588
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.