473,326 Members | 2,111 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,326 software developers and data experts.

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 2355
* 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
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...
7
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....
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. ...
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...
3
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...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
13
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...
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....
2
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...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
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...
1
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.