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

Is this the correct form of a copy constructor?

Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error
....
}

and the GetLength member:

Int16 CString::GetLength()
{
....
}

The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()' discards
qualifiers

I am using the GCC compiler, and I assume the 'const' is the source of the
problem, but everywhere I look, the copy constructor seems to be defined
correctly.

Is this a problem in the compiler, or have I made an error?

Thanks
Dominique

Jul 19 '05 #1
7 3485
Dominique wrote:

Is this a problem in the compiler, or have I made an error?


Yes you have an error! You are calling a non-const method on
a const object 'string.GetLength()'.

BTW: love the cutesy redefinition of short.

Jul 19 '05 #2
Dominique wrote in news:Yn*******************@read1.cgocable.net:
Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error
...
}

and the GetLength member:

Int16 CString::GetLength()
Int16 CString::GetLength() const {
...
}

change the declaration in your CString class too;

class CString
{
// was Int16 CString::GetLength();
Int16 CString::GetLength() const;
};
The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()'
discards qualifiers
the qualifier here is the const applied to the argument of your
copy-ctor.

I am using the GCC compiler, and I assume the 'const' is the source of
the problem, but everywhere I look, the copy constructor seems to be
defined correctly.

Is this a problem in the compiler, or have I made an error?


The compilers right this time.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

"Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca > wrote in
message news:Yn*******************@read1.cgocable.net...
Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error
In this context, the object 'string' is const.
...
}

and the GetLength member:

Int16 CString::GetLength()
{
...
In this context, the 'CString' object (*this) is not const.
}

The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()' discards
qualifiers
It's essentially telling you that you're passing a const
object ('string') to a nonconst member function, which will
treat it (via the nonconst '*this') as a nonconst object,
by 'discarding the const qualifier'. You have just thrown
away the protection against inadvertent modification which
the copy ctor argument type originally provided.
I am using the GCC compiler, and I assume the 'const' is the source of the
problem, but everywhere I look, the copy constructor seems to be defined
correctly.
That's not where the problem is. :-)
Is this a problem in the compiler, or have I made an error?


You need to define 'GetLength()' as a const member function:

Int16 CString::GetLength() const
{
/* etc */
};

And don't forget to change the prototype to match.

*Any* member function which does not modify the object
(such as such 'get data' functions) should be defined
as const.

When I first came to C++, I found these 'const' issues
to be somewhat confusing and 'annoying', but quickly
came to see them for the lifesavers they really are. :-)

HTH,
-Mike

Jul 19 '05 #4

"lilburne" <li******@godzilla.net> wrote in message
news:bn************@ID-203936.news.uni-berlin.de...
Dominique wrote:

Is this a problem in the compiler, or have I made an error?


Yes you have an error! You are calling a non-const method on
a const object 'string.GetLength()'.

BTW: love the cutesy redefinition of short.


How do you know it's a typedef for short?
It might be e.g. an 'int', or perhaps could even
be some complex class in its own right.

-Mike
Jul 19 '05 #5
Mike Wahler <mk******@mkwahler.net> wrote in message
news:yB**************@newsread3.news.pas.earthlink .net...

*Any* member function which does not modify the object
(such as such 'get data' functions) should be defined
as const.


Depending on what exactly you mean by "does not modify the object".

DW

Jul 19 '05 #6
Mike Wahler wrote:
"lilburne" <li******@godzilla.net> wrote in message
news:bn************@ID-203936.news.uni-berlin.de...
Dominique wrote:

Is this a problem in the compiler, or have I made an error?


Yes you have an error! You are calling a non-const method on
a const object 'string.GetLength()'.

BTW: love the cutesy redefinition of short.

How do you know it's a typedef for short?
It might be e.g. an 'int', or perhaps could even
be some complex class in its own right.


Damn you're right:
http://dotnet.di.unipi.it/Content/ss..._1_1Int16.html

Smalltalk anyone?

Jul 19 '05 #7
Thanks all, for the help.

BTW, the Int16 is a 'Palm OS type', typedef'd from a 'signed short'!

Dominique
Jul 19 '05 #8

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
25
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
11
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the...
3
by: puzzlecracker | last post by:
Given, class X { //..... private: auto_ptr<Y> y; };
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.