473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

The const in the copy ctor

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
Jul 22 '05 #1
6 1149
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 explanations welcome

m


Jul 22 '05 #2
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
Jul 22 '05 #3
"mescaline" <ap*****@columbia.edu> wrote in message
news:e5**************************@posting.google.c om...
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/
Jul 22 '05 #4
Jeff Schwab <je******@comcast.net> wrote in message news:<uo********************@comcast.com>...
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.
Jul 22 '05 #5
mescaline wrote:
Jeff Schwab <je******@comcast.net> wrote in message news:<uo********************@comcast.com>...
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.
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

Jul 22 '05 #6
>> 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

Jul 22 '05 #7

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

Similar topics

6
2403
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
11
2306
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...
25
2895
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
2334
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
19
2188
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
4
6670
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
12
6230
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
1
1435
by: zrajbun | last post by:
Hi, I have the following code. struct Object { int m_id; void setId(int const id) { m_id = id;
9
2295
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
0
7112
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
6974
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
7183
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...
1
6852
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...
0
7356
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...
0
5448
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,...
0
3084
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...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
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.