C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The const in the copy ctor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
mescaline
Guest





PostPosted: Sat Dec 27, 2003 11:16 pm    Post subject: The const in the copy ctor Reply with quote



Hi,

I can't understand why there is a "const" for a copy constructor
declaration:
X(const X & X1)

When I deine my Copy ctor to be X(X& X1) i.e. avoiding the const still
things remain the same. However every example I see, there is this
extra "const".

Why is adding the const important here? Any explanations welcome

m
Back to top
Jeff Schwab
Guest





PostPosted: Sat Dec 27, 2003 11:38 pm    Post subject: Re: The const in the copy ctor Reply with quote



mescaline wrote:
Quote:
Hi,

I can't understand why there is a "const" for a copy constructor
declaration:
X(const X & X1)

So you can copy const objects.

Quote:
When I deine my Copy ctor to be X(X& X1) i.e. avoiding the const still
things remain the same. However every example I see, there is this
extra "const".


In your code, you may not be copying any const objects of class X. Even
so, the const qualifier is a nice way of "promising" that the
copy-constructor will not modify the object being copied.

-Jeff

Quote:

Why is adding the const important here? Any explanations welcome

m


Back to top
Ralf
Guest





PostPosted: Sun Dec 28, 2003 12:18 am    Post subject: Re: The const in the copy ctor Reply with quote



The "const" in the copy ctor has two aspects:
1. Normally, it is unusual to change the original while making a copy. With
the "const" you are proposing not to change the original.
2. To have a reference to an constant object makes it possible to copy from
an constant original.

Ralf


www.oop-trainer.de


Back to top
Cy Edmunds
Guest





PostPosted: Sun Dec 28, 2003 1:14 am    Post subject: Re: The const in the copy ctor Reply with quote

"mescaline" <apt2003 (AT) columbia (DOT) edu> wrote

Quote:
Hi,

I can't understand why there is a "const" for a copy constructor
declaration:
X(const X & X1)

When I deine my Copy ctor to be X(X& X1) i.e. avoiding the const still
things remain the same. However every example I see, there is this
extra "const".

Why is adding the const important here? Any explanations welcome

m

Consider:

void afunc(const Fred &f)
{
Fred f2(f);
...
}

If Fred's copy constructor is declared

Fred(Fred&);

then afunc won't compile.

Ordinarily copy constructors and assignment operators do not change the
arguments; i.e.

Fred f2(f); // does not change f
f2 = f; // also does not change f

This is indicated in the signatures which should be

Fred (const Fred &);
and
Fred &operator = (const Fred &);

respectively.

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
mescaline
Guest





PostPosted: Sun Dec 28, 2003 3:21 am    Post subject: Re: The const in the copy ctor Reply with quote

Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote

Quote:
mescaline wrote:
Hi,

I can't understand why there is a "const" for a copy constructor
declaration:
X(const X & X1)

So you can copy const objects.

When I deine my Copy ctor to be X(X& X1) i.e. avoiding the const still
things remain the same. However every example I see, there is this
extra "const".


In your code, you may not be copying any const objects of class X. Even
so, the const qualifier is a nice way of "promising" that the
copy-constructor will not modify the object being copied.

-Jeff


Why is adding the const important here? Any explanation
s welcome

m


Thanks Jeff --
So adding a const is more of a compiler issue than anything else?
i.e.Does adding const improve the way in which the code is compiled?
- m.

Back to top
Jeff Schwab
Guest





PostPosted: Sun Dec 28, 2003 3:25 am    Post subject: Re: The const in the copy ctor Reply with quote

mescaline wrote:
Quote:
Jeff Schwab <jeffplus (AT) comcast (DOT) net> wrote


mescaline wrote:

Hi,

I can't understand why there is a "const" for a copy constructor
declaration:
X(const X & X1)

So you can copy const objects.


When I deine my Copy ctor to be X(X& X1) i.e. avoiding the const still
things remain the same. However every example I see, there is this
extra "const".


In your code, you may not be copying any const objects of class X. Even
so, the const qualifier is a nice way of "promising" that the
copy-constructor will not modify the object being copied.

-Jeff


Why is adding the const important here? Any explanation

s welcome

m



Thanks Jeff --
So adding a const is more of a compiler issue than anything else?

Exactly.

Quote:
i.e.Does adding const improve the way in which the code is compiled?
- m.

Nope. The optimizer usually doesn't even know anything about "const,"
and must determine on its own whether a variable can actually be changed
at run-time. Be careful to whom you tell this, though; there's a lot of
people out there using "const" with religious fervor, believing with all
their soul that this practice makes them efficient programmers. :)

-Jeff


Back to top
Jeff Schwab
Guest





PostPosted: Sun Dec 28, 2003 3:35 am    Post subject: Re: The const in the copy ctor Reply with quote

Quote:
So adding a const is more of a compiler issue than
anything else?

Exactly.

A pang of guilt compells me to add that by using const, you are implying
certain guarantees to the clients of your code. It is true that the
only time you *need* const is to make stuff compile, and that the
compiled code is unlikely to be any better for const having been used.
However, const is also a way to document your code's behavior. It's a
lot like adding a comment that says a variable will not be altered
within a certain section of code. Adding comments doesn't make your
code any more efficient, but it might make the programmers who use it
more efficient.

Ok, guilt assuaged. :)

-Jef


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.